database selection(db_Select) The database contains a mcms_field_data_field_cage_id_vbc table, which has more than 6000 records. I am using the following code to get the content of that table.

function hello_db() {
  $query = db_select('mcms_field_data_field_cage_id_vbc', 'tb');     
  $query-&gt;fields('tb', array('field_cage_id_vbc_target_id'));
  $result = $query-&gt;execute()-&gt;fetchAssoc();
  foreach($result as $record) {
    drupal_set_message($record);
  }
}


The code doesn't show all the records in that table, but a single value. 


    


Why doesn't the code work as expected?
fetchAssoc fetches a single record as an associative array; you're then looping over that one array and setting a message for the single column in that array, field_cage_id_vbc_target_id. Hence just the one result, an integer instead of the expected array(s).

You can use fetchAllAssoc instead:

$result = $query-&gt;execute()-&gt;fetchAllAssoc();

foreach($result as $record){
  drupal_set_message($record);
}