在非对象上调用成员函数result_array()

时间:2014-12-08 15:07:40

标签: mysql codeigniter

请帮助我不知道这是我的模型有什么不对

    public function searchstudents(){
    $establishment_name = $this->input->post('name');
    $address = $this->input->post('address');
    $establishment_name2 = "Nicole's Apartment";


        $query = $this->db->query("SELECT u.id as id, u.first_name as firstname, u.middle_name as                  middlename, u.last_name as lastname, e.name as establishmentname FROM users u, beds b, room r, establishment e WHERE  r.establishment_id = e.id AND u.id = b.student_id  AND b.room_id = r.room_id AND e.name = '$establishment_name2' ");

  $this->db->query($query);

    return $query->result_array(); 

}

当我删除AND e.name ='$ establishment_name2'时它可以工作但是当我包含它时,它会导致调用非对象上的成员函数result_array()。我在mysql中尝试了查询并且它可以工作,但它在codeigniter中不起作用。

3 个答案:

答案 0 :(得分:0)

您需要转义$establishment_name2

$query = $this->db->query("SELECT u.id as id, u.first_name as firstname, u.middle_name as                  middlename, u.last_name as lastname, e.name as establishmentname FROM users u, beds b, room r, establishment e WHERE  r.establishment_id = e.id AND u.id = b.student_id  AND b.room_id = r.room_id AND e.name = ".$this->db->escape($establishment_name2));

答案 1 :(得分:0)

这是一个问题:

$establishment_name2 = "Nicole's Apartment";

变为:

e.name = 'Nicole's Apartment' 
查询中的

使用addslashes()或像Mudshark说的那样逃避

答案 2 :(得分:0)

Noooo,看你可以直接这样做

$this->db->query('SELECT u.id as id, u.first_name as firstname, u.middle_name as                  middlename, u.last_name as lastname, e.name as establishmentname FROM users u, beds b, room r, establishment e WHERE  r.establishment_id = e.id AND u.id = b.student_id  AND b.room_id = r.room_id AND e.name = ?', array($establishment_name2));

$ this-> db-> query()接受3个参数:

1 - Query
2 - Array (with parameters, all parameters are clean)
3 - Escape string.