How to loop over fields from query? I am trying to create nodes from information extracted from a SQL query by using the following:

db_set_active('sage');
$query = db_query('SELECT CLIENT_ID, MASTER_CLIENT_ID, PARTICIPANT_ID FROM sage.CLIENT');
$result = $query-&gt;execute();
foreach($result as $record) {
  $node = new stdClass();
  $node-&gt;title = (string)$record-&gt;participant_id;
  $node-&gt;client_id = (string)$record-&gt;client_id;
  $node-&gt;master_client_id = (string)$record-&gt;master_client_id;
}


However, from my echo statements, I can see that my code never even enters the loop.

I had thought that execute() returns me an object of the query to loop over, but I guess I was wrong.

Could someone please tell me how I can loop over the results of my query and get the fields?
$result = $query-&gt;execute()-&gt;fetchAll();


should do the trick.

There's a lot more info and several more examples in http://api.drupal.org/api/examples/dbtng_example!dbtng_example.module/7