Hi,

in my module i'm using several db_query() and i'd like to check for empty results... something like:


$result = db_query(....);

if(empty($result)) return;

doesn't seem to work. I don't know why exactly but it seems like there's still an object returned from db_query and empty() doesn't see this as empty.

any idea?

--
Frank

Comments

nicholasthompson’s picture

http://api.drupal.org/api/function/db_result/6

You could use that to check if there is at least 1 result or not maybe?

fhelmschrott’s picture

that worked, thanks

if(!db_result($result)) return;

drupalnesia’s picture

Since db_result() removed, then for D7 use:

$results = db_query($sql);

if ($results->num_rows) {
  print 'Total rows is: ' . $results->num_rows; 
}
else {
  print 'Sorry, no result';
}
florentcm’s picture

Undefined property: DatabaseStatementBase::$num_rows

gusantor’s picture

count records as result of db_query 7.x?

you should try

$sql = "SELECT A WORSE CONTENT MANAGEMENT SYSTEM THAN DRUPAL" //(this should retrieve 0 results... :) joke)
$result = db_query($sql);
$how_many_rows = $result->rowCount();

florentcm’s picture

It's the only way I found to test the content returned by $result :
if($result->rowCount()==0) return;

Thank you gusantor !

heyehren’s picture

Thanks florentcm, this worked for me also.