User login

Fastest way to get just one result from a database table in Drupal

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

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.