mysql_num_rows function

Hello,

I have created the following query below which executes fine:

$sql = “select * from q_multchoice where refid = $refid and chapter = $chapters and userid = “.$_SESSION[“uid”].” limit $offset, $ppp”;

$res = mysql_query($sql);

if (mysql_num_rows($res) !=0)
{

}

However, I need to put a check in place when my query returns no results.
I do not want to use the else statement here, I tried…

if (mysql_num_rows($res) == “”)
{

}

but got this error…
mysql_num_rows(): supplied argument is not a valid MySQL result resource

How do I write this?

What makes you say the query executes fine?
Add this debugging code:


$sql = "select * from q_multchoice where refid = $refid and chapter = $chapters and userid = ".$_SESSION["uid"]." limit $offset, $ppp";

//Debug
echo "<p>$sql</p>";

if(!$res = mysql_query($sql)) {
	//Debug
	echo "<p>", mysql_error(), "</p>";
	exit;
}

if (mysql_num_rows($res) !=0) 
{
	//
}


if (mysql_num_rows($res) == "")
{
	//
}

Hi kath,
This error occurs whenever there is an error in your query. Better solution is just echo your query then copy the query and run it in the query editor or you can use phpmyadmin.


echo $sql = "select * from q_multchoice where refid = $refid and chapter = $chapters and userid = ".$_SESSION["uid"]." limit $offset, $ppp";

what you have used below is fine.
if (mysql_num_rows($res) !=0)
{

}

Since manual says:

The number of rows in a result set on success or FALSE on failure.

So AFAIK, so the better check would be


if (!mysql_num_rows($res)){
   //  no records found
}

rather than


if (mysql_num_rows($res) == "")