Hi,

I have created a content-type "customer" with a custom field "phone_number".

I retrieve all my customers like this:

$query = db_select( 'node', 'n' );
  $query
    ->condition( 'type', 'customer' )
    ->fields( 'n' );
  $result = $query->execute();
  $customers = array();
  foreach( $result as $row ) {
    $customers[ $row->nid ] = $row;
  }

But when I do print_r( $customers ), I cannot see any "phone_number". Why?

What is the proper way to get the phone numbers of my customers?

Regards

Comments

rajiv.singh’s picture

$query = db_select( 'node', 'n' );
  $query
    ->condition( 'n.type', 'customer' )
    ->fields('n', array('nid', 'title', 'other_field_name'))
  $result = $query->execute();
  $customers = array();
  foreach( $result as $row ) {
    $customers[ $row->nid ] = $row->title;
  }

bander2’s picture

When you query the database, you are not getting back full nodes, just the data you queried for. You still have to load the full node to access it's fields. I'd try something like this:

$query = db_select( 'node', 'n' );
$query
  ->condition( 'type', 'customer' )
  ->fields( 'nid' );
$result = $query->execute();
$customer_ids = array();
foreach( $result as $row ) {
  $customers_ids[] = $row->nid;
}
$customers = node_load_multiple($customer_ids);

- Brendan

LR’s picture

That works, thank you very much