【问题标题】:Codeigniter Where generate wrong queryCodeigniter 在哪里生成错误的查询
【发布时间】:2017-01-16 17:34:24
【问题描述】:

我尝试获取国家/地区的所有城市,但 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();
}

我为 $country_id 和 $this->db->last_query() 使用 log_message

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\' 谢谢!

【问题讨论】:

    标签: php sql codeigniter where


    【解决方案1】:

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

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

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

    【讨论】: