0
  • PHP 7.2.21
  • Database is on MS SQL, and the database connection is working

I have found and read sqlsrv_num_rows Not Returning Any Value. I have updated the $options based on the solution, but my code still does not return any results.

If I run the query in SQL, I get 8 records, so I know the data is there.

$sql = "SELECT * FROM Question WHERE Category = 'HD';";
$params = array();  
$options =  array( "Scrollable" => 'keyset' ); 
$stmt = sqlsrv_query( $conn, $sql, $params, $options ); 
if( $stmt === false ) { reportSQLError($sql, $params); }
$maxHDQ = sqlsrv_num_rows($stmt);
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

2 Answers2

0

The code itself is ok and should work. What are the values for $stmt and $maxHDQ?

slaakso
  • 8,331
  • 2
  • 16
  • 27
  • ````$stmt = sqlsrv_query( $conn, $sql, $params, $options );```` $stmt is my SQL statement $maxHDQ is where I am trying to store the returned count of rows that $stmt returns Unfortunately the code is not working as $maxHDQ should = 8 after the query runs but it remains null – Erik Bjers Sep 16 '19 at 04:38
0

It is suddenly working

I just removed 'HD' from the query and added it to $params and this seems to have resolved everything.

I am not sure why it is working this way as the final query sent to SQL comes out the same either way I run it, however it does work.

$sql = "SELECT * FROM Question WHERE Category = ?;";
$params = array('HD');  
$options =  array( 'Scrollable' => 'buffered' ); 
$stmt = sqlsrv_query( $conn, $sql, $params, $options ); 
if( $stmt === false ) { reportSQLError($sql, $params); }
$maxHDQ = sqlsrv_num_rows($stmt);