How do I get only one result using db_query()


Here's how to use the Database API in Drupal 7 without writing a MySQL query:

$query = db_select('zipcodes', 'z')
  ->fields('z', array('zip'))
  ->condition('', $city)
  ->range(0, 1)
$result = $query->fetchObject();
print $result->zip;

If you wish to fetch only one result you could use fetchField with db_query to fetch the result, e.g.:

$zip_code = db_query("SELECT zip from {zipcodes} WHERE city = :city LIMIT 1", array(":city" => $city))->fetchField();

You could also fetch the values retrieved from the query's returned result source using options like fetchObject() similar to methods of conventional PHP (mysql_fetch_object) coding like using and get results.

You have to loop your $query, you can't suppose you only have one result with the above given query.

foreach ($query as $row) {
  print $row->zip;

If you know you only have one result, you could call fetchObject on your query ->

$query = db_query("select zip from {zipcodes} where city = :city limit 1", array(":city" => $city))->fetchObject();

print $query->zip should then give you what you want.

Tags: Drupal 7 / Database

Similar questions

Drupal db_or() only returning one result
Can someone please tell me why this code always returns only the last value of the query? I have tried many ways to dynamically build the condition statement for the querying of the database. I have looked at a couple examples and from everything i have seen, this is the way to do it. Any help would be greatly appreciated. Thanks!
Is it possible to only update one panel (or block) as a result of a the URL clicked elsewhere?
Is it possible to only update the content in a particular panel as a result of a URL clicked elsewhere? (i.e no other pages get refreshed.) I have created Views with context filters and would be using them in Panels and would prefer for the whole page not to have to be refreshed. I'm particularly interested in how to retain the zoomed position of a...
FetchObject() only return one result
Select query will always return a result set object of one record.. There are several ways to then retrieve data from that result set, depending on the use case. and also used following query statements I replace the db_condition_placeholder_0 with specify uid. It return me the right result (2 results).
Search API Views only displaying one result
I've just cloned a production site into a staging environment. I copied core and contribute, cloned the repo containing custom modules and themes, cloned the database and copied the solr configuration to a new core. Bringing up the database I configured a new search server pointing to the new core, repointed the index and reindexed the site. All my...
db_select only ever returns one result
I am constructing a query that has to fetch the field collection line item, and it's associated field collection field to get the node id that the field collection field is being used on. The problem is it's only ever returning one single result when I know for sure there are at least two to return. Perhaps a fresh set of eyes can help. Run list fi...
Why is an entity reference view field only showing one view result?
I have a form field on a node create form that is an entity reference, referencing a view that displays all of the appropriate results. The view displays users with a relationship to a content field on the user, so the fields returned are all content. There is also an argument that takes the logged-in user ID. On the node create form, the field in ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.