Agaric Design Collective

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

Average: 1 (1 vote)
By Benjamin Melançon
on 29 Jul
1 comment

Key words and phrases

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

Tags

Description

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

 

Thanks, forgot the name of

Thanks, forgot the name of the function and here it is. Very relevant title for search engine :P.

Posted by Peter (not verified) on Wed, 2008-10-29 09:54
Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.