我试图使用自定义字符串来选择一些值。下面是我的代码
$this->db->from('posted');
$st="infor='rent' AND (typeq='in' OR typeq='out')";
$this->db->where($st);
$q = $this->db->get(); 发生数据库错误 错误号: 1054个未知列‘infor=’rent‘在’WHERE子句‘中选择* FROM (
posted\_ads),其中infor=‘rent’和(typeq=’typeq=‘out’)文件名:infor=‘rent’行号: 330
我认为问题是因为
WHERE `infor='rent'` 当我手动执行这段代码时,它完美地工作了。
WHERE infor='rent' 我该如何摆脱
`` 因为它自动添加了
发布于 2011-09-25 18:48:54
向where()添加第三个参数并将其设置为FALSE
$this->db->from('posted');
$st="infor='rent' AND (typeq='in' OR typeq='out')";
$this->db->where($st, NULL, FALSE);
$q = $this->db->get();
$this->db->where()接受一个可选的第三个参数。如果您将其设置为FALSE,CodeIgniter将不会试图使用backticks保护您的字段名或表名。
发布于 2019-06-11 18:07:12
当解决方案起作用时,我想补充一句:小心!您需要保护查询和转义所有值!如果您喜欢使用查询生成器
$q = $this->db->select('*')->from('posted_ads')
->where('infor', 'rent')
->or_group_start()
->where('typeq', 'in')
->where('typeq', 'out')
->group_end()
->get();这样,Codeigniter就能处理好逃逸问题。
https://stackoverflow.com/questions/7547731
复制相似问题