I have the following code that is throwing an error:

 * Custom content function
 * Get system status from the helpdesk database
 * @return
 *   An key => value pairing of systems and their status
function system_status_sjhc_status_check() {
    //connection info for the helpdesk database
    $helpdeskDB = array(
            'database' => 'mydb',
            'username' => 'myuser', 
            'password' => 'mypass', 
            'host' => 'myhost', 
            'driver' => 'mysql', 
    Database::addConnectionInfo('helpdeskDB', 'default', $helpdeskDB);

    //grab the current light status
    $result = db_select('hd_lights', 'l')
        ->leftJoin('hd_status', 's', 'l.id = s.id')
        ->fields('s', array('stamp', 'message'))
        ->orderBy('`order`', 'ASC')

    db_set_active(); // without the paramater means set back to the default for the site

    return $result;

The error I'm receiving is:

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

and its being thrown on this line:

    ->fields('s', array('stamp', 'message'))

I can't figure out what I've done wrong, everything looks OK to me and the code runs fine without the join and additional table fields.

Any thoughts?


SelectQuery::join(), SelectQuery::leftJoin() etc. don't return the query (they return the alias to the created JOIN), so they can't be chained.

Just separate your code out like this:

$query = db_select('hd_lights', 'l')
  ->fields('s', array('stamp', 'message'))
  ->orderBy('`order`', 'ASC');

$query->leftJoin('hd_status', 's', 'l.id = s.id');

$result = $query->execute();
  • I'm grateful too ! Got crazy before reading your answer... – Kojo May 30 '14 at 10:36

This is how you suppose to write db_select with join (leftJoin in your case)

   //grab the current light status
    $result = db_select('hd_lights', 'l')
    $result->leftJoin('hd_status', 's', 'l.id = s.id');
    $result->fields('s', array('stamp', 'message'))
        ->orderBy('`order`', 'ASC')

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.