How do I check if the following code has results?
I tried the empty function but it always returns FALSE
$query = db_select('my_table', 't');
$query->condition('code', 10)
->fields('t', array('cid'));
$result = $query->execute();
If I'm correct the result of the execute() call should be a DatabaseStatementInterface which has a rowCount function.
var_dump($result->rowCount());
Commented
Oct 23, 2011 at 12:18
If you print var_dump($result);
you will see that $result
contains the query and other variables so it's never empty.
You can put $result
in a foreach
loop to see the results.
foreach ($result as $record) {
var_dump($record);
}
Inside the foreach
loop, you can print the results with this code.
foreach ($result as $record) {
print $record->nid;
print $record->title;
}
If there is only one record as result use mysql_fetch_array()
to handle the record.
foreach ($result as $record) { var_dump($record); }