joshgarrod Posted March 28, 2012 Share Posted March 28, 2012 Hi, I am having an issue with a piece of code. I am getting the following error, any ideas please? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ... <?php include "scripts/connect.php"; $last = $_GET['ref']; // This will be the ID of the page echo "the ref number is $last"; $query = mysql_query("SELECT * FROM news WHERE `ref` = '$last'"); if(mysql_num_rows($query)==0){ die("Something went wrong, contact Administrator!"); }else{ while($info = mysql_fetch_array($query)){ $title = $info ['title']; echo "<p><a href=\"..news.php\" target=\"_blank\">$title >></a></p>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259860-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/ Share on other sites More sharing options...
cpd Posted March 28, 2012 Share Posted March 28, 2012 $query = mysql_query("SELECT * FROM news WHERE `ref` = '$last'"); This is where your error is. Ensure everything is spelled correctly etc. Quote Link to comment https://forums.phpfreaks.com/topic/259860-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-1331804 Share on other sites More sharing options...
soma56 Posted March 28, 2012 Share Posted March 28, 2012 Change: echo "the ref number is $last"; to echo "the ref number is ".$last; This isn't what's causing the error though. And as far as the mysql statement: $query = mysql_query("SELECT * FROM news WHERE `ref` = '$last'"); I'd take out the ` and either remove them completely or change them to ' $query = mysql_query("SELECT * FROM news WHERE ref = '$last'"); Make sure the table name (news) and the column within the table (ref) match perfectly in the DB. Quote Link to comment https://forums.phpfreaks.com/topic/259860-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-1331877 Share on other sites More sharing options...
PFMaBiSmAd Posted March 28, 2012 Share Posted March 28, 2012 That error means that your query failed due to an error of some kind (no database connection, permission problem, wrong table/column names, un-escaped data that breaks the sql syntax/allows sql injection,...) If you use mysql_error(), php/mysql will tell you why it failed. You can temporarily change your mysql_query() statement to the following to get some debugging information from it - $query = mysql_query("SELECT * FROM news WHERE `ref` = '$last'") or die('Query failed: ' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/259860-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-1331879 Share on other sites More sharing options...
AyKay47 Posted March 28, 2012 Share Posted March 28, 2012 Change: echo "the ref number is $last"; to echo "the ref number is ".$last; this is not needed, variables are interpolated inside of double quotes, what the OP has is fine. I'd take out the ` and either remove them completely or change them to ' why? back-ticks are perfectly valid and are typically encouraged to wrap identifiers. OP, debug the query a little to see what is going wrong: $sql = "SELECT * FROM news WHERE `ref` = '$last'"; $query = mysql_query($sql); if(!$query) { echo $sql . "<br />" . mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/259860-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-1331880 Share on other sites More sharing options...
joshgarrod Posted March 28, 2012 Author Share Posted March 28, 2012 Thanks for your help everyone, problem solved. Stupid typo on DB name. Quote Link to comment https://forums.phpfreaks.com/topic/259860-warning-mysql_num_rows-supplied-argument-is-not-a-valid-mysql-result-resourc/#findComment-1331887 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.