db_select() returns an array I have just started using Drupal 7 and db_select().

if I have a result from db_select() (e.g. $result = $query-&gt;execute(), why can I only loop through this result once?

It appears as if there is a cursor that is not reset following a foreach loop.

So I tried to find a function that would convert this result into a multi dimensional array (e.g. $result = $result-&gt;fetchAllAssoc()) but I have found nothing.

Instead, at the moment I have to loop through the result to create an array of row objects to use later through the code (if i need to loop through it more than once)

/* this returns an array of row objects that you can use like any other array but seems as though there should be an API function to do this for me as part of the database class ? */

$array = array();
foreach($result as $row) {
  $array[] = $row;
}


Surely there must be an API function to do this already.
I don't have a Drupal 7 dev environment handy to try this on, but I think you are looking for fetchAllAssoc(), as you mentioned. This page has some query examples that are helpful.

When you call fetchAllAssoc(), you need to specify a field name as the primary key of the array, like:

 $result-&gt;fetchAllAssoc($field);


It also appears that fetchAll() will work (but apparently it is not the recommended solution).