Codeigniter: Call to a member function result_array() on a non-object

前端 未结 1 523
青春惊慌失措
青春惊慌失措 2021-01-22 09:51

I\'m using Codeigniter to build a webapp and I received this error:

Fatal error: Call to a member function result_array() on a 
non-object in /var/www/applicatio         


        
1条回答
  •  失恋的感觉
    2021-01-22 10:27

    since get_all_contacts is already using the result() function in your model, you can't also use the result_array() function in your controller. result_array() would be a method of the $query object. The quick and dirty way to get this working(which might break other stuff if its also using the get_all_contacts method) would be to change your get all contacts function to the following:

    function get_all_contacts() {
        $query = $this->db->query('SELECT * FROM person');
        return $query;
    }
    

    however, if you want to be smarter about it and not risk breaking other stuff, you can pass a param from the controller, and only return query if its set like so:

    REVISED CONTROLLER LINE**

    $allContacts = $this->people_model->get_all_contacts(true);
    

    REVISED MODEL CODE

    function get_all_contacts($special = false) {
        $query = $this->db->query('SELECT * FROM person');
        if($special)
        {
            return $query;
        }
        return $query->result();
    }
    

    0 讨论(0)
提交回复
热议问题