Merge pull request #256 from v-dareck/github37_has_rows

Github 37 sqlsrv_has_rows
This commit is contained in:
Jenny Tam 2017-01-30 16:29:24 -08:00 committed by GitHub
commit 1fe9c605d1
2 changed files with 108 additions and 1 deletions

View file

@ -957,7 +957,7 @@ PHP_FUNCTION( sqlsrv_has_rows )
throw ss::SSException();
}
if( !stmt->fetch_called ) {
if( !stmt->has_rows && !stmt->fetch_called ) {
determine_stmt_has_rows( stmt TSRMLS_CC );
}

View file

@ -0,0 +1,107 @@
--TEST--
sqlsrv_has_rows() using a forward and scrollable cursor
--DESCRIPTION--
This test calls sqlsrv_has_rows multiple times. Previously, multiple calls
with a forward cursor would advance the cursor. Subsequent fetch calls
would then fail.
--SKIPIF--
--FILE--
<?php
require_once("autonomous_setup.php");
// Connect
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( !$conn ) { die( print_r( sqlsrv_errors(), true)); }
// Create database
sqlsrv_query($conn,"CREATE DATABASE ". $dbName) ?: die();
// Create table
$query = "CREATE TABLE $tableName (ID VARCHAR(10))";
$stmt = sqlsrv_query($conn, $query);
$query = "INSERT INTO $tableName VALUES ('1998.1'),('-2004'),('2016'),('4.2EUR')";
$stmt = sqlsrv_query($conn, $query) ?: die( print_r( sqlsrv_errors(), true) );
// Fetch data using forward only cursor
$query = "SELECT ID FROM $tableName";
$stmt = sqlsrv_query($conn, $query)
?: die( print_r(sqlsrv_errors(), true));
// repeated calls should return true and fetch should work.
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
if (sqlsrv_has_rows($stmt)) {
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo $row[0]."\n";
}
}
// Fetch data using a scrollable cursor
$stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered"))
?: die( print_r(sqlsrv_errors(), true));
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
if (sqlsrv_has_rows($stmt)) {
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))
{
echo $row[0]."\n";
}
}
$query = "SELECT ID FROM $tableName where ID='nomatch'";
$stmt = sqlsrv_query($conn, $query)
?: die( print_r(sqlsrv_errors(), true));
// repeated calls should return false if there are no rows.
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
// Fetch data using a scrollable cursor
$stmt = sqlsrv_query($conn, $query, [], array("Scrollable"=>"buffered"))
?: die( print_r(sqlsrv_errors(), true));
// repeated calls should return false if there are no rows.
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
echo "Has Rows?" . (sqlsrv_has_rows($stmt) ? " Yes!" : " NO!") . "\n";
// DROP database
$stmt = sqlsrv_query($conn,"DROP DATABASE ". $dbName);
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
print "Done"
?>
--EXPECT--
Has Rows? Yes!
Has Rows? Yes!
Has Rows? Yes!
Has Rows? Yes!
1998.1
-2004
2016
4.2EUR
Has Rows? Yes!
Has Rows? Yes!
Has Rows? Yes!
Has Rows? Yes!
1998.1
-2004
2016
4.2EUR
Has Rows? NO!
Has Rows? NO!
Has Rows? NO!
Has Rows? NO!
Done