php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28068 mssql_num_rows() is returning an invalid handle
Submitted: 2004-04-19 22:01 UTC Modified: 2004-04-28 00:13 UTC
From: oooacooo at yahoo dot com Assigned: fmk (profile)
Status: Not a bug Package: MSSQL related
PHP Version: 4.3.4 OS: Windows 2K Server; Apache 2.0.45
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oooacooo at yahoo dot com
New email:
PHP Version: OS:

 

 [2004-04-19 22:01 UTC] oooacooo at yahoo dot com
Description:
------------
When using mssql_execute() with PHP version 4.3.2 mssql_num_rows() will return 0, when no records are returned from a stored procedure.

When using version 4.3.4 and above mssql_num_rows() will return "supplied argument is not a valid MS SQL-result
resource"

I tested this by running the code successfully with the php_mssql.dll which ships with 4.3.2 and then swapped php_mssql.dll with the one that ships with 4.3.4 and restarted Apache.

Reproduce code:
---------------
$validEpisode = mssql_query("spValidateEpisodeDate " . $AccountID . ",'" . $inputVisitDate . "'",$db);

        if(mssql_num_rows($validEpisode) > 0)
		{
			$row = mssql_fetch_object($validEpisode);
			if($row->Episode_ID == "")
				$errorMsg = "There is no episode for this account that covers the visit date entered.<br>" . $errorMsg;
			else
				$EpisodeID = $row->Episode_ID;
		}

Expected result:
----------------
If no records are found I would expect "if(mssql_num_rows($validEpisode) > 0)" to return true.

Actual result:
--------------
"if(mssql_num_rows($validEpisode) > 0)" throws an error "supplied argument is not a valid MS SQL-result
resource"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-20 09:11 UTC] derick@php.net
Assigning to the maintainer, as this is obviously wrong (and BC breaking) behavior.
 [2004-04-20 23:38 UTC] fmk@php.net
The result from mssql_query() is not a valid resource. The function might just return TRUE if the storred procedure was executed with success. Not having access to the stored procedure makes it difficult to tell why this is happening.

You should check the result with is_resource() before calling mssql_num_rows() (and any other mssql_function for that matter) if you want to avoid this error message, or make sure your storred procedure returns a result set (even with 0 rows) each time.
 [2004-04-23 19:26 UTC] oooacooo at yahoo dot com
The content of the stored procedure is irrelevant because if you can see from my description it worked with build 4.3.2 and after installing 4.3.4 it failed.  I then relaced php_mssql.dll with the one from the 4.3.2 build and it worked again.

If a rewrite of my application or the stored procedure to fix the issue is your work around that is fine, but build 4.3.4 still implemented a backward compatablity issue.
 [2004-04-23 21:11 UTC] fmk@php.net
When an invalid resource handle is passed to mssql_num_rows() it is supposed to throw an error. Your problem is that you are not checking the resource handle returned by the query function before calling mssql_num_rows()

mssql_query() can return true, false or a resource handle and the value must be checked before calling any other mssql_* functions to avoid error messages.

You can check why the call to mssql_query returns different values for two different versions of PHP, but I doubt that it has anything to do with PHP.
 [2004-04-27 21:42 UTC] oooacooo at yahoo dot com
I tried checking the result of mssql_query and it is returning true.  When I pass it to mssql_num_rows() it states that it is not a valid MS SQL-result resource.  I am at a loss on how to get around this.
 [2004-04-28 00:13 UTC] fmk@php.net
When mssql_query() returns true it means that the SQL statement(s) was executed without any errors and they did not return any data (result sets).

You can't pass a true value to mssql_num_rows() it requires a resource handle. You can check if the returned value is a resource valye with this code:

$rs = mssql_query(....);
if (is_resource($rs)) {
  // continue
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC