30 July 2023

To retrieve a single value from the database in Drupal 7, you can use the db_query() function along with the appropriate SQL query and utilize the fetchField() method to obtain the desired result.

Source code viewer
  1. // SQL query to get the single value.
  2. $query = "SELECT $column_name FROM {" . $table_name . "} WHERE $condition";
  3.  
  4. // Execute the query using db_query() function.
  5. $result = db_query($query);
  6.  
  7. // Fetch the value from the result.
  8. $value = $result->fetchField();
  9.  
  10. // Now, $value contains the single value you wanted to retrieve.
Programming Language: PHP