Codeigniter 在哪里生成错误的查询

Codeigniter Where generate wrong query

我尝试获取国家/地区的所有城市,但 codeigniter 生成错误 sql,为什么? 这是我的模特

private $cities    = "cities";

public function get_cities_by_country($country_id){
    $this->db->select('id, city');
    $this->db->where('country_id', $country_id);
    $this->db->from($this->cities);
    return $this->db->get()->result_array();
}

我用 log_message 表示 $country_id 和 $this->db->last_query()

INFO  - 2017-01-16 18:30:25 --> '7'
INFO  - 2017-01-16 18:30:25 --> 'SELECT `id`, `city`
FROM (`cities`)
WHERE `country_id` =  \'7\'
AND `status` =  1'

我认为这是一个问题 country_id = \'7\' 谢谢!

CI 试图转义 where 值,您可以通过将 FALSE 作为可选参数传递给 where 子句来避免它:

    $this->db->where('country_id', $country_id, FALSE);

在此处阅读更多内容:https://www.codeigniter.com/userguide2/database/active_record.html