7 database.inc public DatabaseStatementInterface::fetchCol($index = 0)

Returns an entire single column of a result set as an indexed array.

Note that this method will run the result set to the end.

Parameters

$index: The index of the column number to fetch.

Return value

An indexed array, or an empty array if there is no result set.

3 methods override DatabaseStatementInterface::fetchCol()
DatabaseStatementBase::fetchCol in includes/database/database.inc
Returns an entire single column of a result set as an indexed array.
DatabaseStatementEmpty::fetchCol in includes/database/database.inc
Returns an entire single column of a result set as an indexed array.
DatabaseStatementPrefetch::fetchCol in includes/database/prefetch.inc
Returns an entire single column of a result set as an indexed array.

File

includes/database/database.inc, line 2084
Core systems for the database layer.

Class

DatabaseStatementInterface
Represents a prepared statement.

Code

public function fetchCol($index = 0);

Comments

See the other DatabaseStatementInterface methods here:
http://api.drupal.org/api/drupal/includes!database!database.inc/interfac...

It would be v useful and trivial to implement a 2nd, optional parameter so you could fetch the values of one column keyed by the values of another.

Without this I'm resorting to fetchAllAssoc($key_column), then looping to replace the StdClass objects created with the value needed. Seems messy.

I thought the poster was asking if we could include the column name instead of the index, so I could do something like $query->fetchCol('entity_id'); if I wanted, e.g., a list of entities affected by a given field. That seems like it would be much more helpful than just providing a numerical index. (Of course, you could just change your select query to select only the column name you want, but then where would be the fun in that?)