| 5 database.mysqli.inc | db_result($result, $row = 0) |
| 5 database.pgsql.inc | db_result($result, $row = 0) |
| 5 database.mysql.inc | db_result($result, |
| 6 database.mysql.inc | db_result($result) |
| 6 database.pgsql.inc | db_result($result) |
| 6 database.mysqli.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()
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;
}
Login or register to post comments
Comments
Count rows
Count rows in a table using db_result:
<?php$countrows = db_result(db_query("SELECT COUNT(*) FROM {my_table}"));
?>
db_result removed from Drupal 7
Drupal6:
$val = db_result(db_query({...}))may be rewritten in Drupal7 as
$val = db_query({...})->fetchField();Count rows in a table
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);
Correct but the num_rows
Correct but the num_rows property may only be used if mysqli connection is active.
Resets resource ID
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.