wierd sql issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

wierd sql issue

Post by kingconnections »

So I posted a question about this a while back and have been using this method ever since, but today I ran into a strange issue.

This involves mssql_query function and evaluating the results. Normally I do it as such:

Code: Select all

if ($result === false) 
{ 
  // error 
} 
elseif ($result === true) 
{ 
  // no rows 
} 
else 
{ 
  // you have rows 
}
According to the php.net manual this should and has been working:
Returns: A MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error.

Today I came across a result that was not in the database so it returned 0 rows. But it did have a resource id so was evlauated as data.

Here is a print out of the number of rows and the queries that were executed.

Code: Select all

query Resource id #5 = SELECT Name FROM Computer WHERE (Name = 'FSILBIT201') AND (idComputer NOT IN (SELECT idComputer FROM computerMaintenance WHERE InMaintenanceNow = '1')) -- num rows =1
query Resource id #6 = SELECT Name FROM Computer WHERE (Name = 'FSILHFTR01') AND (idComputer NOT IN (SELECT idComputer FROM computerMaintenance WHERE InMaintenanceNow = '1')) -- num rows =1
query Resource id #7 = SELECT Name FROM Computer WHERE (Name = 'GOORCACCAPP1') AND (idComputer NOT IN (SELECT idComputer FROM computerMaintenance WHERE InMaintenanceNow = '1')) -- num rows =0
Any ideas? I expected for the num rows =0 to return a TRUE instead they returned a resource id?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: wierd sql issue

Post by volka »

kingconnections wrote:According to the php.net manual this should and has been working:
Returns: A MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error.
What version of the manual?
http://de2.php.net/manual/en/function.mysql-query.php wrote:Return Values

For SELECT, SHOW, DESCRIBE or EXPLAIN statements, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Hmm not sure says for php 3,4,5

link is here:

http://us3.php.net/manual/en/function.mssql-query.php

And this is concerning MS SQL
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Hi,

Shouldnt it just be:

Code: Select all

if ($result == false) 
{
== not ===
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:oops: me blind . mssql != mysql
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Actually I tried === and == and both had the same effect. I thought so too after I tested it.
Post Reply