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().

Parameters

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

Return value

The resulting field or FALSE.

Related topics

120 calls to db_result()
actions_function_lookup in includes/actions.inc
Given an md5 hash of a function name, return the function name.
aggregator_block in modules/aggregator/aggregator.module
Implementation of hook_block().
block_add_block_form_validate in modules/block/block.admin.inc
block_admin_configure_validate in modules/block/block.admin.inc
block_flush_caches in modules/block/block.module
Implementation of hook_flush_caches().

... See full list

File

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

Code

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

Comments

joseonate’s picture

Count rows in a table using db_result:

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

Drupal6:

$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);

print_r($resultSet->num_rows);
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);
   //drupal_set_message(db_result($result));
  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);
   
   drupal_set_message($update);
  } 
 return; 

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.