4.6.x database.pgsql.inc db_result($result, $row = 0)
4.6.x database.mysql.inc db_result($result, $row = 0)
4.7.x database.pgsql.inc db_result($result, $row = 0)
4.7.x database.mysqli.inc db_result($result, $row = 0)
4.7.x database.mysql.inc db_result($result, $row = 0)
5.x database.pgsql.inc db_result($result, $row = 0)
5.x database.mysqli.inc db_result($result, $row = 0)
5.x database.mysql.inc db_result($result, $row = 0)
6.x database.pgsql.inc db_result($result)
6.x database.mysqli.inc db_result($result)
6.x database.mysql.inc db_result($result)

Return an individual result field from the previous query.

Only use this function if exactly one field is being selected; otherwise, use db_fetch_object() or db_fetch_array().


$result: A database query result resource, as returned from db_query().

Return value

The resulting field or FALSE.

Related topics

74 calls to db_result()
actions_function_lookup in includes/actions.inc
Given an md5 hash of a function name, return the function name.
comment_multiple_delete_confirm in modules/comment/comment.admin.inc
List the selected comments and verify that the admin really wants to delete them.
comment_num_all in modules/comment/comment.module
Get comment count for a node.
comment_num_new in modules/comment/comment.module
Get number of new comments for current user and specified node.
comment_num_replies in modules/comment/comment.module
Get replies count for a comment.

... See full list


includes/database.pgsql.inc, line 206
Database interface code for PostgreSQL database servers.


function db_result($result) {
  if ($result && pg_num_rows($result) > 0) {
    $array = pg_fetch_row($result);
    return $array[0];
  return FALSE;


joseonate’s picture

Count rows in a table using db_result:

  $countrows = db_result(db_query("SELECT COUNT(*) FROM {my_table}"));
dman’s picture


$val = db_result(db_query({...}))

may be rewritten in Drupal7 as

$val = db_query({...})->fetchField();
faruq1256’s picture

Drupal 6:

You can get the num_rows value from query ResultSet. Here is the Example:

$resultSet = db_query($query);

vensires’s picture

Correct but the num_rows property may only be used if mysqli connection is active.

mykmallett’s picture

Does this function make the $result variable unusable? I have a query that checks if a row exists, if not then it should print an error. If there is a row, carry on with the rest of the query and return

if ($salesdesk) {  
  $result = db_query("SELECT nid FROM {content_type_salesdesk} WHERE nid = %d AND field_status_value = 2", $salesdesk);
  if (db_result($result) == NULL) {
   drupal_set_message('Could not send products to Salesdesk. Please check the the Salesdesk is registered with the Quote Engine');
 } else {
  $result = db_query("SELECT nid FROM {content_type_salesdesk} WHERE field_status_value = 2");
  while ($row = db_fetch_array($result)) {
   $node = node_load($row['nid']);
   $title = $node->title;
   $domain = $node->field_domain[0]['value'];
   $api_key = $node->field_salesdesk_key[0]['value'];
   $update = wuwo_salesdesk_services_pushproduct($title, $api_key, $domain);

Now if I give the query a saledesk that doesn't return a row then I get the error message. If I give it one that does exists, it doesn't do anything.

If I put the db_result into a drupal_set_message before the conditional then it tells kicks out the error message (as if it doesn't exist), when the row definitely does exist. Which indicates that the mysql resource id is being reset or set to false.

The rest of the script works without this conditional.