User login

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

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

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;
}
?>

Resolution

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.
  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • 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>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options

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