3

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

         $query->__toString()

         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.
6

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

<?php
  // Retrieve all records into an indexed array of stdClass bjects.
  $result->fetchAll();
 // Retrieve all records into an associative array keyed by the field in the result specified.
  $result->fetchAllAssoc($field);

  // Retrieve a 2-column result set as an associative array of field 1 => field 2.
  $result->fetchAllKeyed();
 // 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.
  $result->fetchCol();
  // Column number can be specified otherwise defaults to first column
  $result->fetchCol($column_index);
 ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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