Jump to content

[SOLVED] PHP log warning for mysql_num_rows


oskom

Recommended Posts

Hello all,

In trying to clean up some PHP errors, I frequently get a "PHP Warning"(mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/ftp/path/to/file/content.php on line 30). Here's the code in question:

$result = mysql_query('query here');
if(mysql_num_rows($result) == 0) { //this is the suspect line

I'm pretty sure the fix is:

$result = mysql_query('query here');
if(mysql_num_rows($result) == false) { //0 changed to false

In reading PHP.net, http://us2.php.net/mysql_num_rows, it states for returned values "The number of rows in a result set on success, or FALSE on failure."

 

Am I on the right track? I'm asking only because success with bug-checking using the error log tends to be spotty at best.

Point well taken, cooldude832, about sticking raw data into a query...note to self. On the error, I should say that I'm getting all the desired results from this query. Essentially, the if statement is checking if the ID passed in a URL string exists as an ID for a row in the database. If it returns nothing, an error message displays. It all works fine, if tenuously, as the PHP error suggests.

 

Let me ask, however, if doing this would be just as well...

$result = mysql_query("SELECT * FROM content WHERE contentID = ".$_REQUEST['contentID']);
if(!$result) {

 

Or is it 6-or-one-half-dozen?

<?php
$_REQUEST['contentID'] = (int)$_REQUEST['contentID'] < 0 ? 0 : (int)$_REQUEST['contentID'];

if( $_REQUEST['contentID'] > 0 ) {
   $result = mysql_query("SELECT * FROM content WHERE contentID = '{$_REQUEST['contentID']}'");
   if(!$result) {
      // ERROR
   }
} else {
   // Need an ID
}
?>

Thanks, rab. By the way(stinking novice question to follow), the first part of this code I get...

$_REQUEST['contentID'] = (int)$_REQUEST['contentID']

It's this part that tweaks my gray matter...

 < 0 ? 0 : (int)$_REQUEST['contentID'];

I've seen this syntax before but never quite understood it's function. Is that a short-form of an if/else statement?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.