I query my database using the following query API code, but Drupal tells me that the result is a non-object. I can't find any reason to it

in what situation does db_query return anything but?

The error:

Fatal error: Call to a member function execute() on a non-object

the code:

for ($counter = 1; $counter <= 12; $counter++) {
  $query[$counter] = db_query("SELECT `foo` FROM `{bar}` WHERE DATE(FROM_UNIXTIME(`thing_$counter`)) = CURRENT_DATE");
  $results = $query['$counter']->execute()

The SQL works and produced results when I ran it in PHPmyAdmin.

The answer in this exchange looked tantalizing:

$date = date_create();
$date_to_test_against =  $date->format('U');
 $query[$counter] = db_query("SELECT `foo` FROM `{bar}` WHERE `thing_$counter` = $date_to_test_against");

however that produced the same result, calling execute on a non-object.

I know I don't have enough experience with this API yet. Can someone please point me to what I'm missing?

  • You have single quotes around ['$counter'], $counter won't evaluate. Also, try using db_select instead. Feb 8 '13 at 9:04

First, in the third line of your first snippet, you're using $query['$counter']. Remove the quotes.

Second, a db_query return value is a query already executed, so you want $results = $query[$counter]->fetchAll();

  • First, AAUUUGH! I thought I'd corrected that. This actually points out that my larger problem was having a large function of code written while learning this API, and all the resulting Frankencode. Thanks, too, to @david-thomas for pointing that out. Second -- awesome! I was getting an entirely different impression from the documentation. I've pared down my work to a test function, and it works. Now I'll break up my function & rewrite.
    – Complex
    Feb 8 '13 at 14:53

In the case of db_query, what you get back from it is a resource (it is just a wrapper around php's mysql_query.

But the answer depends on if you're in Drupal 6 or 7

Drupal 6:

In order to turn that into an object, you need to run db_fetch_object on the result, and if you have multiple results you'd need to loop through it.

For example:

    $sql = "SELECT buckles FROM foo WHERE llama = 'cheese'";
    $result = db_query($sql);

    while ($rowObj = db_fetch_object($result)) {
        echo $rowObj->buckles;

Drupal 7

Use the nifty db_select api! The examples on that page pretty good, but basically it builds a query and then returns something that's very similar to a PDO object (see SelectQuery::execute for the following example)

  $matches = array();
  $query = db_select('table')
     ->fields('table', array('field_name'));

  foreach ($query->execute() as $row) {
     $matches[$row->relation_type] = $row->label;
  • Thank you, that's very helpful cuz I'll have to do this for Drupal 6 in the future! I'm in Drupal 7 now. I'm using db_query b/c my queries are static, and b/c it let me test my mysql to make sure it wasn't the problem. The documentation suggests db_query for a static query like this; do you think db_select is actually a better pick in this case?
    – Complex
    Feb 8 '13 at 15:00
  • I personally prefer to use the select api since I might wind up having to change it later, but you certainly can use db_query if that's what you want to do.
    – Cthos
    Feb 8 '13 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.