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

About Craig Lotter

Software developer, husband and dad to two young ladies. Writer behind An Exploring South African. I don't have time for myself any more.