Skip to content

Commit

Permalink
Merge pull request #333 from v-kaywon/numrow_null_result
Browse files Browse the repository at this point in the history
fix crash from getting the numrow of a null buffered result set
  • Loading branch information
yukiwongky authored Mar 23, 2017
2 parents 7434d06 + d68300f commit 566a2da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion source/shared/core_results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,13 @@ SQLLEN sqlsrv_buffered_result_set::row_count( TSRMLS_D )
{
last_error = NULL;

return zend_hash_num_elements( cache );
if ( cache ) {
return zend_hash_num_elements( cache );
}
else {
// returning -1 to represent getting the rowcount of an empty result set
return -1;
}
}

// private functions
Expand Down
31 changes: 31 additions & 0 deletions test/sqlsrv/srv_330_numrow_null_buffered_result_set.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
GitHub issue #330 - get numrow of null buffered result set
--DESCRIPTION--
A variation of the example in GitHub issue 330. A -1 value returned as numrow of a null buffered result set.
--SKIPIF--
--FILE--
<?php
require_once("tools.inc");

require_once("autonomous_setup.php");

// Connect
$conn = sqlsrv_connect($serverName, $connectionInfo) ?: FatalError("Failed to connect");

$stmt = sqlsrv_query($conn, "IF EXISTS (SELECT * FROM [sys].[objects] WHERE (name LIKE 'non_existent_table_name%') AND type in (N'U'))
BEGIN
select 0
END", [], ['Scrollable' => SQLSRV_CURSOR_CLIENT_BUFFERED]);

if ($stmt) {
$hasRows = sqlsrv_has_rows($stmt);
$numRows = sqlsrv_num_rows($stmt);
echo "hasRows: ";
var_dump($hasRows);
echo "numRows: ";
var_dump($numRows);
}
?>
--EXPECT--
hasRows: bool(false)
numRows: int(-1)

0 comments on commit 566a2da

Please sign in to comment.