The return from db_query()
is a resource that you can iterate over, it's never a single value.
The way to get a single field from a query is with the fetchField()
method:
$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();
If you've got more than one result then you can iterate over it like so:
foreach ($result as $row) {
// Do something with $row...
}
or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include:
-
Fetches the next row and returns it as an object.
-
Returns the result set as an associative array keyed by the given field.
-
Returns the entire result set as a single associative array.
-
Fetches the next row and returns it as an associative array.
-
Returns an entire single column of a result set as an indexed array.
-
Returns a single field from the next record of a result set.