How to obtain contents from a drupal 7 query? I am trying to perform the below simple query:

$query = db_select('sage.USER', 'USER')
  -&gt;fields('USER', array('USER_ID', 'OFFICE_ID'));
$result = $query-&gt;execute();


I would like to then pull out the 'office_id' that is returned by the query, which I have been told can be done with the following:

$office = $result-&gt;office_id;


However, when I do 'print_r($office)' or 'echo $office', it would seem the $office string is empty. The database is not empty and contains the appropriate values.

Could someone please how I can pull out the contents of a drupal 7 query?
execute() returns a resource you can iterate over in a foreach loop, e.g: 

foreach ($result as $record) {
  $office_id = $record-&gt;office_id;
}


In your case you probably want to use the fetchObject() method to actually retrieve the first record returned from the query:

$result = $query-&gt;execute()-&gt;fetchObject();
$office = $result-&gt;office_id;