Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected result for sqlsrv_num_rows #330

Closed
jansor opened this issue Mar 19, 2017 · 3 comments
Closed

Unexpected result for sqlsrv_num_rows #330

jansor opened this issue Mar 19, 2017 · 3 comments
Assignees

Comments

@jansor
Copy link

jansor commented Mar 19, 2017

Hi, I am not sure is this is a bug or the behavior is correct but when i execute below code the web server crashes. It provides me with Bad Gateway

The original script that i used was much bigger but when i execute this test script i got the same issue:
`<?php
$server = "localhost";
$connectionInfo = ["UID" => "localhost", "PWD" => "localhost", 'Database' => 'localhost'];
$conn = sqlsrv_connect($server, $connectionInfo);

$stmt = sqlsrv_query($conn, "IF EXISTS (SELECT * FROM [sys].[objects] WHERE (name LIKE 'tbl_Test%') 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);
var_dump($hasRows, $numRows);
}
?>`

Following scenario is expexted, when the table name like tbl_Test exists it would return the amount of tables that are found, hens sqlsrv_has_rows would be true and sqlsrv_num_rows would return the amount
When no tables are found sqlsrv_has_rows must be false, sqlsrv_num_rows would be zero.

Thanks to look into this

PS i am using following setup
IIS10, PHP7.1.2 and SQL Driver 4.1.7-preview on an SQL 2016 server

@yukiwongky
Copy link
Contributor

Hello @jansor, we are able to reproduce the issue. Thank you for reporting to us.

The behavior that you expect when table name like 'tbl_Test%' exists is incorrect. For example, say you have three tables 'tbl_Test1', 'tbl_Test2' and 'tbl_Test3'. The query you have will return 0 due to the select 0. Thus sqlsrv_num_rows will return 1 and not 3 as you have expected. If you want to count the number of tables with name like 'tbl_Test%', you can try the following query:
SELECT COUNT(*) from sys.objects where (name LIKE 'tbl_Test%') AND (type = 'U')

@jansor
Copy link
Author

jansor commented Mar 21, 2017

Yes you are correct, in my testing previous i had the select query from the if statement also inside the select 0 statement. Thanks for the confirmation its a bug, i was confused with the result set.

@yukiwongky
Copy link
Contributor

The fix should be in the latest release (4.1.8-preview). Please check to see if your problem still exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants