PHP: How to Check if mysql_query() has returned a Result Set Programming 09 MAR 2011

mysql_query() is the powerful PHP function tasked with handling the execution of all SQL statements against the currently active database connection string. It handles all variations of SQL commands, including SELECT, INSERT, UPDATE, DELETE, etc, and always returns false if the command execution should fail. However, only on SELECT does it bother returning a valid result set – the rest of the time it returns true.

So how do we know if a mysql_query() function call has returned a result set?

Actually the answer is pretty simple – we simply check if the returned object is a boolean with the basic is_bool function!

In practice:

$result = mysql_query("SELECT * FROM table");
if (is_bool($result) == false){
//we have a result set
} else {
//we don't have a result set


a bowl of red apples on a chopping board

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • Vaibhav

    Although I don’t know how to do it, but this is not a foolproof method. Imagine a select query returns a blank result set then also the is_bool($result) will be false. 

    • No, you don’t quite grasp it. An empty result set is a valid result set, meaning the boolean is set to true. This is not a mysql_num_rows test – it is a test to indicate whether or not the SQL statement succeeded.