Call to a member function result() using CodeIgniter

安稳与你 提交于 2020-01-15 09:54:15

问题


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



来源:https://stackoverflow.com/questions/9490598/call-to-a-member-function-result-using-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!