oskom Posted December 4, 2007 Share Posted December 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 your query maybe failed thats why your getting that error post here your actual sql statement Quote Link to comment Share on other sites More sharing options...
oskom Posted December 4, 2007 Author Share Posted December 4, 2007 The code for the query is: $result = mysql_query("SELECT * FROM content WHERE contentID = ".$_REQUEST['contentID'].""); Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 $result = mysql_query("SELECT * FROM content WHERE contentID = ".$_REQUEST['contentID']."") or die (mysql_error()); try and print here the result Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 4, 2007 Share Posted December 4, 2007 you shouldn't use the raw data from any form like REQUEST GET POST etc Quote Link to comment Share on other sites More sharing options...
rab Posted December 4, 2007 Share Posted December 4, 2007 Filter that variable as an integer and quote it in the SQL command. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 4, 2007 Share Posted December 4, 2007 I am surprised there is not a sticky post concerning that error. It has been asked about four times in the past couple of hours. Quote Link to comment Share on other sites More sharing options...
oskom Posted December 4, 2007 Author Share Posted December 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
rab Posted December 4, 2007 Share Posted December 4, 2007 <?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 } ?> Quote Link to comment Share on other sites More sharing options...
oskom Posted December 4, 2007 Author Share Posted December 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.