Fatal error: Call to a member function fields() on a non-object I have the following code that is throwing an error:

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

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

    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:

    -&gt;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')
  -&gt;fields('l')
  -&gt;fields('s', array('stamp', 'message'))
  -&gt;orderBy('`order`', 'ASC');

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

$result = $query-&gt;execute();