MSSQL_QUERY not returning True

MSSQL_QUERY not returning True

am 08.10.2007 05:52:18 von BethH

According to php.net, mssql_query is supposed to return true when no
rows are returned. I'm actually getting an empty resource identifier
instead of true.

The table is empty, there are no rows in it at all, so mssql_query
should always return true, but it isn't. Am I missing something?
Here's my code:

$StrQuery = "SELECT * FROM BookMarks WHERE UserName='" . $ID . "'";
$Result = mssql_query($StrQuery);

I'm not sure how I could have messed it up. I tried looking for bool-
true (=== TRUE), but it didn't catch it. So then I tried any true (==
TRUE), and it worked. So then I tried looking for a bool (is_bool)
and it didn't catch it. Finally, I had it just print the result and
it gives me a resource identifier.

It keeps trucking through as if it had actually found something. But
when I do this:

while ($Row = mssql_fetch_assoc($Result)){
[code to make a table with the data]
};

nothing happens, because, of course, there are no actual rows, only
imaginary ones.

I've changed my code to check if the array is false before starting
the while loop, but that seems like a weird way to go just to see if
there's any data to work with.

Any Suggestions?

Re: MSSQL_QUERY not returning True

am 08.10.2007 13:34:46 von colin.mckinnon

On 8 Oct, 04:52, BethH wrote:
> According to php.net, mssql_query is supposed to return true when no
> rows are returned. I'm actually getting an empty resource identifier
> instead of true.
>

I've not checked the manual, but I'd be very surprised if that was the
case. Certainly it should return false if the query fails, and a valid
resource if successful. Note that in PHP in a boolean context, a non-
zero value is dynamically retyped to a true boolean and zero to false.

> I'm not sure how I could have messed it up. I tried looking for bool-
> true (=== TRUE), but it didn't catch it. So then I tried any true (==
> TRUE), and it worked. So then I tried looking for a bool (is_bool)
> and it didn't catch it. Finally, I had it just print the result and
> it gives me a resource identifier.
>

Try:

if ( $Result ) {
// query worked, but we don't know how many rows it returned until
we go looking
$count=0;
while ($Row = mssql_fetch_assoc($Result)){
$count++;
[code to make a table with the data]

}
if (!$count) {
print "No rows returned";
}
} else {
// query has failed - but should already have thrown an error
}

Alternatively use the mssql_num_rows() function.

C.

Re: MSSQL_QUERY not returning True

am 08.10.2007 19:23:33 von zeldorblat

On Oct 7, 11:52 pm, BethH wrote:
> According to php.net, mssql_query is supposed to return true when no
> rows are returned. I'm actually getting an empty resource identifier
> instead of true.
>
> The table is empty, there are no rows in it at all, so mssql_query
> should always return true, but it isn't. Am I missing something?
> Here's my code:
>
> $StrQuery = "SELECT * FROM BookMarks WHERE UserName='" . $ID . "'";
> $Result = mssql_query($StrQuery);
>
> I'm not sure how I could have messed it up. I tried looking for bool-
> true (=== TRUE), but it didn't catch it. So then I tried any true (==
> TRUE), and it worked. So then I tried looking for a bool (is_bool)
> and it didn't catch it. Finally, I had it just print the result and
> it gives me a resource identifier.
>
> It keeps trucking through as if it had actually found something. But
> when I do this:
>
> while ($Row = mssql_fetch_assoc($Result)){
> [code to make a table with the data]
>
> };
>
> nothing happens, because, of course, there are no actual rows, only
> imaginary ones.
>
> I've changed my code to check if the array is false before starting
> the while loop, but that seems like a weird way to go just to see if
> there's any data to work with.
>
> Any Suggestions?

You're getting confused on the definition of "when no rows are
returned." They're talking about a query that doesn't actually return
rows, like insert, update, or delete. A query that returns a result
(like select) will always return a resource identifier, regardless of
how many rows are in the result.