Home ›
Fastest way to get just one result from a database table in DrupalFastest way to get just one result from a database table in Drupal
Submitted by Benjamin Melançon on July 29, 2008 - 8:02am
To get a single value result – one row from a just one column – the Drupal function for that is db_result(), which takes the result of db_query() as an argument.
http://api.drupal.org/api/function/db_result/5
(The same function works for Drupals 4.7 and 6.)
So the fastest way to get a single result (please pardon the awkwardly named function) would look like this:
<?php
function field_placement_txt_field_get_cck($node_type, $txt_field_name) {
$cck = db_result(db_query("SELECT cck FROM {field_placement_txt_field} WHERE type = '%s' AND txt = '%s'", $node_type, $txt_field_name));
return $cck;
}
?>
Update in Drupal 7 (haven't tested this yet, may be typos):
<?php
$cck = db_query("SELECT cck FROM {field_placement_txt_field} WHERE type = :node_type AND txt = :txt_field_name", array(':node_type' => $node_type, ':txt_field_name' => $txt_field_name))->fetchField();
?>
More commonly you may want ->fetchAllAssoc(). See db_query() in the Drupal API, especially people's comments.
Searched words:
db_fetch single result string from Drupal database
db get one value
short function to return one database data or, rather, datum
db_result Drupal database query result
fetch single result
Comments
Thanks, forgot the name of
Thanks, forgot the name of the function and here it is. Very relevant title for search engine :P.
Post new comment