Make a direct database query Basically I want to run this query against the current database and retrieve all node titles with node type of foo_type.

SELECT title FROM node WHERE type='foo_type' ORDER BY title ASC


How can I do this using db_query or any similar function? I tried doing the below but $result is empty.

$result = db_query("SELECT title FROM node WHERE type='foo_type' ORDER BY title ASC");


Please note any D6 and D7 differences if there are any.
For Drupal 6:

$result = db_query("SELECT title FROM node WHERE type = '%s' ORDER BY title ASC", 'foo_type');
$titles = array();
while ($row = db_fetch_object($result)) {
  $titles[] = $row-&gt;title;
}


For Drupal 7 you have a bunch of options

Static:

$args = array(':type' =&gt; 'foo_type');
$titles = db_query("SELECT title FROM node WHERE type = :type ORDER BY title ASC", $args)-&gt;fetchCol();


Dynamic:

$titles = db_select('node')
  -&gt;fields('node', array('title'))
  -&gt;condition('type', 'foo_type')
  -&gt;orderBy('title')
  -&gt;execute()
  -&gt;fetchCol();


And a couple of less-direct methods...

EntityFieldQuery:

$query = new \EntityFieldQuery();
$query-&gt;entityCondition('entity_type', 'node')
  -&gt;entityCondition('type', 'foo_type')
  -&gt;propertyOrderBy('title');

$results = $query-&gt;execute();
if (!empty($results['node'])) {
  $nodes = node_load_multiple(array_keys($results['node']));
  $titles = array_map(function($x) { return $x-&gt;title; }, $nodes);
}


API (deprecated):

$nodes = entity_load('node', FALSE, array('type' =&gt; 'foo_type'));
$titles = array_map(function($x) { return $x-&gt;title; }, $nodes);