Re: MySQL resource
| From: | David Otton | Date: | Fri, 12 Oct 2001 11:52:00 +0000 |
| Subject: | Re: MySQL resource | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-70921@lists.php.net to get a copy of this message | ||
On Fri, 12 Oct 2001 11:20:20 +0100, you wrote:
>> $result = mysql_query($query, $this->connection);
>>
>> if ($result) {
>> echo (mysql_num_rows($result));
>> }
>>
>> The way I read the manual, the if() only fires off if the query is
>> executed, and then by definition mysql_num_rows() should contain at
>> least a 0. So why do I get this error?
>>
>> Warning: Supplied argument is not a valid MySQL result resource in
>> C:\blah\class_test.inc on line 195
>>
>> If I echo($result) it comes back as
>>
>> 1
>
>The result of the query function is a "resource" NOT a boolean.
It appears that it can be an int in some situations. I guess I'd have
to check source code to be certain, but...
if I do this :
echo(mysql_query("SELECT ...."));
I get 'Resource id#4' echoed, while if I do this :
echo(mysql_query("INSERT ...."));
I get a straight '1'.
The '1' is returned when you do an UPDATE/INSERT/DELETE, and the
'Resource id' when you do a select. It's a nasty one, but it is
documented (kinda) in the manual pages for mysql_affected_rows(). So
now, my arbitary "run stuff" function looks something like this :
function RunSQLCount($query) {
$result = mysql_query($query, $connection)
or die('error');
if ($result) {
if (stristr($query,'SELECT')) {
$num_rows = mysql_num_rows($result);
mysql_free_result($result);
} else {
$num_rows = mysql_affected_rows($connection);
}
return($num_rows);
}
return(-1);
}
That stristr() is a little dodgy, but I have control over the data
going in.
(Note that _num_rows() uses the result set, but _affected_rows() uses
the connection).
>Trust this helps,
It would have, thanks, but I figured it out this morning before I
checked email.
djo