Can someone please tell me why this code always returns only the last value of the query? I have tried many ways to dynamically build the condition statement for the querying of the database. I have looked at a couple examples and from everything i have seen, this is the way to do it. Any help would be greatly appreciated. Thanks!

function get_drupal_school_info($schoolIds) {
$or = db_or();
foreach ($schoolIds as $index => $data){
    ->condition('f.field_external_id_value', $data['entity_id'],'=')
    ->condition('f.bundle', 'school')

$query = db_select('field_data_field_external_id', 'f');
$query->leftJoin('field_data_body', 'b', 'f.entity_id = b.entity_id');
$query->leftJoin('field_data_field_partner_code', 'pc', 'b.entity_id = pc.entity_id');
    ->fields('f', array('entity_id'))

return $query->execute()->fetch();

It's, because you are calling fetch(). That method fetches a single result.

If you want all, you can simply loop over the return value of $query->execute() or use fetchAll() or one of the many related helper functions.

| improve this answer | |

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.