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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.