Tag Archives: result set

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

How to Reuse a MySQL Result Set CodeUnit 15 FEB 2011

To reuse an existing result set returned from a query is actually pretty simple thanks to PHP’s mysql_data_seek function and the fact that mysql_query returns a buffered result set by default.

So essentially all we want to do is rewind the entire pointer right to the start in order to reuse the set.

And to do this?

mysql_data_seek($result, 0);