Skip to main content
Participating Frequently
January 6, 2011
Answered

Checking if MySQL table is empty

  • January 6, 2011
  • 2 replies
  • 1117 views

Hey,

I want to make sure that if my MySQL table is empty users would receive "There is no information to display." in their browsers. Here's how I've already tried to do it:

if (!mysql_query("SELECT * FROM Table",$con))
{
    echo "<b>There is no information to display.</b><br /><br />";
}
else

{

    echo "<b>We have something to show you.</b><br /><br />";

}

Although MySQL table is empty it returns "We have something to show you." as if there are records in that DB table.

What do I do wrong? Is there another way to check if the MySQL table is empty?

* BTW - I'm using UniServer.

Cheers,

DissidentPJ

This topic has been closed for replies.
Correct answer bregent

mysql_query only returns false on error. An empty recordset is not an error. Using a rowcount method as you have mentioned in your second post.

2 replies

bregentCorrect answer
Participating Frequently
January 6, 2011

mysql_query only returns false on error. An empty recordset is not an error. Using a rowcount method as you have mentioned in your second post.

Participating Frequently
January 6, 2011

Found a way around it:

$sql = mysql_query("SELECT * FROM Table",$con);

if (mysql_num_rows($sql) == 0)
{
    echo "<b>There is no information to display.</b><br /><br />";
}
else

{

    echo "<b>We have something to show you.</b><br /><br />";

}