简体   繁体   中英

Drupal 7 - What is the most appropriate way to retrieve all the rows of a particular column?

I am using the following code in an attempt to retrieve all the rows of a particular column in Drupal 7:

// Use Database API to retrieve current posts.
    $query = db_select('field_data_field_phone_number', 'n');
    $query->fields('n', array('field_phone_number_value'));

    // Place queried data into an array
    $phone_numbers = $query->execute()->fetchAssoc();

I thought this was enough to retrieve an entire column, but when I use the following line to display the query, there are no values to display:

drupal_set_message( '<pre>'.print_r($phone_numbers, true).'</pre>');

I know for a fact that there are relevant values in the table as I have checked using MySQLWorkbench.

Any help would be greatly appreciated, thanks!

Solved!

$phone_numbers = $query->execute()->fetchAssoc();

Should be:

$phone_numbers = $query->execute()->fetchCol();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM