Select query will always return a result set object of one record.. There are several ways to then retrieve data from that result set, depending on the use case.

   user's flaged entity id:

  $query = db_select('flagging', 'f');
  $query->condition('f.uid', $data['uid'], '=');
  $query->fields('f', array('entity_id'));
  $result = $query->execute()->fetchObject();

and also used following query statements


         SELECT f.entity_id AS entity_id  FROM {flagging} f WHERE  (f.uid = :db_condition_placeholder_0) 

I replace the db_condition_placeholder_0 with specify uid.
It return me the right result (2 results).

   **$query->execute()->fetchAssoc()**`, return one result.

1 Answer 1


In Drupal 7, there are fetch methods that can help to avoid loops like that. From http://drupal.org/node/310072:

  // Retrieve all records into an indexed array of stdClass bjects.
 // Retrieve all records into an associative array keyed by the field in the result specified.

  // Retrieve a 2-column result set as an associative array of field 1 => field 2.
 // You can also specify which two fields to use by specifying the column numbers for each field
  $result->fetchAllKeyed(0,2); // would be field 0 => field 2
  $result->fetchAllKeyed(1,0); // would be field 1 => field 0

  // Retrieve a 1-column result set as one single array.
  // Column number can be specified otherwise defaults to first column

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.