Call to a member function result() using CodeIgnit

2019-08-08 21:55发布

问题:

I am having a strange issue with mysql and codeigniter. I am getting the following error message:

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

Controller:

class Event extends Client_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Event_Model', '', TRUE);
    }

    function pending()
    {
        $data['query'] = $this->Event_Model->get_events_list("pending");
        $this->load->view('event/pending', $data);
    }   

}

Model:

class Event_Model extends Client_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function get_events_list($event_status = '')
    {
        $query = $this->db->query("SELECT * FROM tbl_events WHERE event_status= ? ORDER BY event_id DESC", array($event_status));

        return $query->result();// Error is on this line
    }
}

Autoload:

$autoload['libraries'] = array('database', 'session');

Any help is appreciated. I have some doubts that my mysql might time-out?

回答1:

The error is likely due to the fact that the query (and resulting result() object) are empty. Ensure that the query actually returns at least one record before attempting to use the result() object:

if($query->num_rows() > 0){
    return $query->result();
}


回答2:

Usually it's because your query is wrong. Remember that result() is boolean and if nothing comes back it is a fatal error since in the SQL server an error returned