Fields

Last updated on
11 March 2021

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

This documentation needs work. See "Help improve this page" in the sidebar.

Add a field

To add a field to the Select query, use the addField() method:

$title_field = $query->addField('n', 'title', 'my_title');

The above code will instruct the query to select the "title" field of the table with alias "n", and give it an alias of "my_title". If no alias is specified, one will be generated automatically. In the vast majority of cases the generated alias will simply be the field name. In this example, that would be "title". If that alias already exists, the alias will be the table name and field name. In this example, that would be "n_title". If that alias already exists, a counter will be added to the alias until an unused alias is found, such as "n_title_2".

Note that if you are creating and populating the query yourself and do not specify an alias and the default alias is not available, there is almost certainly a bug in your code. If you are writing a hook_query_alter() implementation, however, you cannot know with certainty what aliases are already in use so you should always use the generated alias.

Add multiple fields

To select multiple fields, call addField() multiple times in the order desired. Note that in most cases the order of fields should not matter, and if it does then there is likely a flaw in the business logic of the module.

As an alternate shorthand, you can use the fields() method to add multiple fields at once.

$query->fields('n', array('nid', 'title', 'created', 'uid'));

The above method is equivalent to calling addField() four times, once for each field. However, fields() does not support specifying an alias for a field. It also returns the query object itself so that the method may be chained rather than returning any generated aliases. If you need to know the generated alias, either use addField() or use getFields() to access the raw internal fields structure.  Note: getfields() will not return any information if fields were set by $query->fields('n');

Calling fields() with no field list will result in a "SELECT *" query.

$query->fields('n');

That will result in "n.*" being included in the field list of the query. Note that no aliases will be generated. If a table using SELECT * contains a field that is also specified directly from another table, it is possible for a field name collision to occur in the result set. In that case, the result set will only contain one of the fields with the common name. For that reason the SELECT * usage is discouraged.

Return only one field using fetchField

Use the method fetchField to return only one field with the query, e.g. as follows. (slightly silly example)

$query = db_select('node', 'n');
$query->condition('n.nid', 123);
$query->addField('n', 'title');
$result = $query->execute();
return $result->fetchField();

Help improve this page

Page status: Needs work

You can: