Search code examples
phpmysqlwarningsmysql-num-rows

mysql_num_rows() gives a BOOL value


I have this query in my PHP:

<?php
$comments_list = mysql_query("SELECT * FROM comments ORDER BY `date` DESC, `time` DESC LIMIT 5");
echo mysql_error();
if (mysql_num_rows($comments_list) != 0)
{
    echo('<ul>');
    while ($comment = mysql_fetch_array($comments_list))
    {
        echo('<li>'.substr($comment['content'],0,70).'</li>');
    }
    echo('</ul>');
}
else
{
    echo('<center>No comment.</center>');
}
?>

And it gives this error if my table is empty:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.9\www\admin\index.php on line 101

EDIT: I was querying LIMIT 5 before the ORDER BY. Now it's fixed and I added the echo mysql_error() as edited in the question.


Solution

  • Because $comments_list is false, use mysql_error() to see what is wrong.

    $comments_list = mysql_query("SELECT * FROM comments LIMIT 5 ORDER BY `date` DESC, `time` DESC");
    if (!$comments_list ) {
        die('Invalid query: ' . mysql_error());
    }