Problem with db_select not returning records sometimes Works:

$rows = db_select('notification_queue', 'nq')
    fields('nq', array('created_time', 'uid', 'notification_type', 'delivery_method','latest_time', 'earliest_time'))
    -&gt;groupBy('uid')
    -&gt;groupBy('notification_type')
    -&gt;groupBy('delivery_method')
    -&gt;addExpression('COUNT(*)', 'count')
    -&gt;execute();


Does not work:

$rows = db_select('notification_queue', 'nq');
$rows-&gt;fields('nq', array('created_time', 'uid', 'notification_type', 'delivery_method', 'latest_time', 'earliest_time'));
$rows-&gt;groupBy('uid');
$rows-&gt;groupBy('notification_type');
$rows-&gt;groupBy('delivery_method');
$rows-&gt;execute();


I'm testing that something is returned with a simple foreach loop:

foreach($rows as $row) {
    msg($row); //msg is a simple wrapper for drupal set message
}


The reason I need to do things the second way is that I have to use addExpression and it won't work if try and use it in the first way.

I've tried this:

$rows = db_select('notification_queue', 'nq');
$rows-&gt;fields('nq', array('created_time', 'uid', 'notification_type', 'delivery_method', 'latest_time', 'earliest_time'));
$rows-&gt;groupBy('uid');
$rows-&gt;groupBy('notification_type');
$rows-&gt;groupBy('delivery_method');
$rows-&gt;addExpression('COUNT(*)', 'count');
$rows-&gt;execute();


And I've checked that the resulting query is correct (it is, I've tried using it with MySQL directly and it does exactly what I want). However, I can't get access to the rows using Drupal.

What am I doing wrong?
Turns out addExpression does not return the object, just the alias of the expression. Solution:

$query = db_select('notification_queue', 'nq')
    -&gt;fields('nq', array('created_time', 'uid', 'notification_type', 'delivery_method', 'latest_time', 'earliest_time'))        
    -&gt;groupBy('uid')
    -&gt;groupBy('notification_type')
    -&gt;groupBy('delivery_method');

$query-&gt;addExpression('COUNT(*)', 'num');
$rows = $query-&gt;execute();