Jump to content

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resourc


Recommended Posts

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>";

					}
					}
		?>

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. 

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());

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();
}

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.