Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of wcc_wtaylor
wcc_wtaylor

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'

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of Dave BaldwinDave Baldwin🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of wcc_wtaylorwcc_wtaylor

ASKER

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.

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.

Avatar of Dave BaldwinDave Baldwin🇺🇸

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.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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.

Avatar of Dave BaldwinDave Baldwin🇺🇸

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

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

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Dave BaldwinDave Baldwin🇺🇸

That's fine, it wasn't a complaint.  Yes, the drivers and methods have changed.  Thanks for the points.
PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.