The issue is most likely around the creation of your SELECT statement at the following lines:
$query = "SELECT T1.stu_id, T1.grd_lvl, T1.name_f, T1.name_l ,T2.common_name, RTRIM(T1.Pre2000StudentLogin) AS networklogin,RTRIM(T1.Pre2000StudentLogin) + '@school.org' AS gaccount" .
" FROM [Students] AS T1 INNER JOIN [Campus] AS T2 ON T1.campus_id = T2.campus_id" .
" WHERE name_l LIKE '%s' AND T1.LastUpdate = (SELECT MAX(LastUpdate) FROM [Students])";
if ($campus <> '999'){
$query = $query . " AND T1.campus_id = '%s' ORDER BY name_l, name_f";
$tsql = sprintf($query,$lname,$campus);
}ELSE IF(is_int($studentId)) { $query = $query . "AND T1.stu_id = '%s' ORDER BY name_l, name_f"; $tsql = sprintf($query, $studentId, $lname);
}ELSE{
$query = $query . " ORDER BY name_l, name_f";
$tsql = sprintf($query,$lname);
}
$stmt = sqlsrv_query( $conn, $tsql);
So use the debugger, or add some debug code, to see exactly what you are supposed to extract from the database, and what you actually receive.