Re: MySQL resource
| From: | DL Neil | Date: | Fri, 12 Oct 2001 10:20:20 +0000 |
| Subject: | Re: MySQL resource | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-70911@lists.php.net to get a copy of this message | ||
> $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.
This situation is confusing because the prototype, ie the letter of the law, in the manual
(http://www.php.net/manual/en/function.mysql-query.php) says "resource" but the
explanatory text
then goes on to talk about TRUE and FALSE. You will find this elsewhere in PHP where the casual
approach to data-typing allows use/abuse of what some have been trained to see as 'rules'!
In fact if you drop in various echo/prints to monitor the process:
(warning: I've hacked out this code from within an existing application and flattened out the
function calls - so it may not run if you cut-and-paste, as-is)
$WhereClause = 'Where phase=1 Order by search';
$tablename = "tblPList_tst";
if (DEBUG) echo " tbl=$tablename~";
$query = "select * from $tablename ".$WhereClause;
if (DEBUG) echo "<br>Fn MySQLdbRead: Query=".$query;
$queryresourceresult = mysql_query($query, $dbLinkId);
if (DEBUG) echo "~Result=".$queryresourceresult;
$num_rows_read = mysql_num_rows($queryresourceresult);
if (DEBUG) echo "~ Num rows read=$num_rows_read";
In the browser you will see...
2001-10-12 10:46:06 Executing mainline B.php
FnConnectToDatabase~
Fn MySQLdbOpen: LinkIdentifier =Resource id #1 Whereas Select db result =1
FnFetchFirst~ tbl=tblPList_tst~
Fn MySQLdbRead: Query=select * from tblPList_tst Where phase=1 Order by search~Result=Resource id
#2~Num rows read=1~
Thus the problematic code (as above)
if ($result) {
would, in this stub/example, become
if ("Resource id #2") {
I must admit, most of the time I do NOT bother to check the query result (of a Select). [greater
minds might point out some shock-horror that I have casually ignored] Instead I look at the Num-Rows
function (in the case of a MySQL Select command) because I am happy to accept a zero-or-more records
found response.
Of course, if the query is being constructed dynamically, and particularly from some user/foreign
input, then this is much too casual even if the code previously implements 'solid'
filtering and
error-checking precautions! In such as case, more attention is required - as might be appropriate
during first stage prototyping in any case. During first-run debugging I might look to see if the
QueryResourceResult is false, because that would indicate a syntactically invalid query (tip: most
of the time I have checked that by using a command line or MySQL admin tool to prototype the dbquery
conversation and when first writing the code I'm copying-and-pasting from there into PHP). In
which
case one would invert the test and make it thoroughly type-specific:
if ( $result === false ) { echo "invalid query - rejected by MySQL"; }
Trust this helps,
=dn