SQLSRV_FETCH_ARRAY in PHP Only Returning 1 Row then stopping

I'm new to the sqlsrv system, been using the mssql system for a while and never had issues.  I'm modifying the old mssql to sqlsrv and I'm only getting one row returned in my while loop.  When I run the query directly into my SQL Server Management I get 33 rows.  You can see, when I run the sqlsrv_num_rows I also get the count of 33 but the fetch is dying after 1 row.  I'm connecting to my DB inside the db_conx.php file which is included at the top of this page.

Please advise
<?PHP
error_reporting(E_ALL ^ E_NOTICE);	

	include '/www/vhosts/localhost/attendance/components/db_conx.php';
	

	$course_attendance_query = "SELECT ClassDates.ClassId 
			,ClassDates.ClassDate 
			,UserId 
			,ClassDates.DateRecord 
			,AttendValue 
		FROM attendance_bb.dbo.AttendRecs
			,attendance_bb.dbo.ClassDates 
		WHERE attendance_bb.dbo.AttendRecs.DateRecord = attendance_bb.dbo.ClassDates.DateRecord 
			AND SUBSTRING(attendance_bb.dbo.ClassDates.ClassId,2,LEN(attendance_bb.dbo.ClassDates.ClassId)-3) IN (34290, 34294, 34295)
			AND attendance_bb.dbo.ClassDates.ClassDate >= '2015-04-27' 
			AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('coordinator_%') 
			AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('dd_%') 
			AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('dpc_%') 
			AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('15%') 
		ORDER BY SUBSTRING(attendance_bb.dbo.ClassDates.ClassId,2,LEN(attendance_bb.dbo.ClassDates.ClassId)-3) ASC
			,attendance_bb.dbo.ClassDates.ClassDate ASC 
	";
	echo "$course_attendance_query<br>\n\n";

	$bbsql1_sqlsrv_database='attendance_bb';
	$bbsql1_sqlsrv_connect = connect_to_bbsql1($bbsql1_sqlsrv_database);
	
	$params = array();
	$options =  array( "Scrollable" => 'buffered' );
	$course_attendance_result = sqlsrv_query($bbsql1_sqlsrv_connect, $course_attendance_query, $params, $options);
	if (!$course_attendance_result)
		{
			echo "There is a problem with course_attendance_query: \n $course_attendance_query \n\n<br><br>";
			if (($errors = sqlsrv_errors()) != null) 
				{
					foreach( $errors as $error ) 
						{
							echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
							echo "code: ".$error[ 'code']."<br />";
							echo "message: ".$error[ 'message']."<br />";
						}
				}	   
		   exit;
			echo "\n\n\n";
		}
	echo "Beginning of var_dump:<br>";
	var_dump($course_attendance_result);
	echo "<br>End of var_dump<br><Br>";
	echo "Results:<br>" . $course_attendance_result . "<Br><br>";
	$n = 1;
	$viewable_clicks = 0;
	$course_attendance_num_rows = sqlsrv_num_rows($course_attendance_result);
	echo "course_attendance_num_rows =" . $course_attendance_num_rows . "----()<br>";
	while ($course_attendance_row = sqlsrv_fetch_array($course_attendance_result, SQLSRV_FETCH_ASSOC))
		{
			$course_attendance_row = array_map('trim', $course_attendance_row);
			$ClassId = $course_attendance_row['ClassId'];
			$ClassId_break = explode("_",$ClassId);
			$crsmain_pk1 = $ClassId_break[1];
			$ClassDate = date("Y-m-d", strtotime($course_attendance_row['ClassDate']));
			$UserId = $course_attendance_row['UserId'];
			$DateRecord = $course_attendance_row['DateRecord'];
			$AttendValue = $course_attendance_row['AttendValue'];
	
			$bbsql1_sqlsrv_database='bblearn';
			$bbsql1_sqlsrv_connect = connect_to_bbsql1($bbsql1_sqlsrv_database);

			$find_student_info_query = "SELECT firstname
					,lastname
				FROM users
				WHERE batch_uid = '$UserId'
			";
			
			echo "$n - $find_student_info_query<br>";
			
			$n++;
			
		}

?>

Open in new window


Here is what I'm getting on my page:
Connected to AS400 correctly

Connected to MySQL correctly

Able to connect to bblearn on sql1-64

Able to connect to attendance on SQLSRV01\SQLSERVER

SELECT ClassDates.ClassId ,ClassDates.ClassDate ,UserId ,ClassDates.DateRecord ,AttendValue FROM attendance_bb.dbo.AttendRecs ,attendance_bb.dbo.ClassDates WHERE attendance_bb.dbo.AttendRecs.DateRecord = attendance_bb.dbo.ClassDates.DateRecord AND SUBSTRING(attendance_bb.dbo.ClassDates.ClassId,2,LEN(attendance_bb.dbo.ClassDates.ClassId)-3) IN (34290, 34294, 34295) AND attendance_bb.dbo.ClassDates.ClassDate >= '2015-04-27' AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('coordinator_%') AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('dd_%') AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('dpc_%') AND attendance_bb.dbo.AttendRecs.UserId NOT LIKE ('15%') ORDER BY SUBSTRING(attendance_bb.dbo.ClassDates.ClassId,2,LEN(attendance_bb.dbo.ClassDates.ClassId)-3) ASC ,attendance_bb.dbo.ClassDates.ClassDate ASC
Able to connect to attendance_bb on bbsql1-64

Beginning of var_dump:
resource(9) of type (SQL Server Statement)
End of var_dump

Results:
Resource id #9

course_attendance_num_rows =33----()
Able to connect to bblearn on bbsql1-64

1 - SELECT firstname ,lastname FROM users WHERE batch_uid = 'XXXXXXX'
wcc_wtaylorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
You are trying to re-use 'bbsql1_sqlsrv_connect' inside the 'while' loop and that doesn't work, it kills the original connection.  Change the name for 'bbsql1_sqlsrv_connect' inside the 'while' loop.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wcc_wtaylorAuthor Commented:
Thanks Dave,
How do I change the database that I'm querying then?  I'm using an attendance_db in the first part then bblearn in the second.
0
wcc_wtaylorAuthor Commented:
Dave,
I figured it out... I changed the variable and it worked.

Thanks a lot, this has been driving me crazy.  I'm used to in MYSQL and MSSQL to be able to just change the DB and not have to redo the connection string.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Dave BaldwinFixer of ProblemsCommented:
Actually that would not have worked in either one of those drivers inside a loop.  You could change the database when you have used up the data but you could never have re-used it inside a loop like that.
0
wcc_wtaylorAuthor Commented:
Dave,

The old way I was doing I was using the mssql_select_db and changing it back and forth inside the while loop and it still worked fine.
0
Dave BaldwinFixer of ProblemsCommented:
There is a lot of code that is not in your post above.  Anything that overwrites the results from the first query will kill the 'while' loop.  And there is a good chance that the 'sqlsrv' driver handles things differently than the 'mssql' driver.  For example, the database selection is part of the basic connection string in 'sqlsrv' and not a separate statement or command like it is with 'mysql' or 'mssql'.  http://php.net/manual/en/function.sqlsrv-connect.php  And in the newer 'mysqli' driver, the database selection is also part of the connection command.  http://php.net/manual/en/mysqli.construct.php
0
wcc_wtaylorAuthor Commented:
Thanks Dave.  I'm learning that all this is different and so not knowing and asking is how to learn.  Yes, there is a lot of code not in my example here, but this was the basis of me trying to get information at a basic level then expanding.  There was no reason to post ALL 200+ lines of code when the first 80 lines is where my main problem was.

Thanks a lot for the information and guidance.  

WT
0
Dave BaldwinFixer of ProblemsCommented:
That's fine, it wasn't a complaint.  Yes, the drivers and methods have changed.  Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.