2011-09-25 46 views
7

我试图使用自定义字符串选择一些值。下面是我的代码

$this->db->from('posted'); 
    $st="infor='rent' AND (typeq='in' OR typeq='out')"; 
    $this->db->where($st); 
    $q = $this->db->get(); 

数据库出错

Error Number: 1054 

Unknown column ‘infor=‘rent’’ in ‘where clause’ 
SELECT * FROM (`posted_ads`) WHERE `infor=‘rent’` AND (typeq=‘in’ 
OR typeq=‘out’) 
Filename: C:\wamp\www\parklot\system\database\DB_driver.php 
Line Number: 330 

我认为这个问题是怎么把的

WHERE `infor='rent'` 

当我manualy执行此代码它完美的作品。

WHERE infor='rent' 

我如何摆脱

`` 

,因为它会自动添加

回答

19

添加第三个参数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() accep可选的第三个参数。如果将其设置为FALSE,CodeIgniter将不会尝试使用反引号来保护您的字段或表名。

CodeIgniter Documentation

+0

如果你经常做类似的事情,有用的创造,其周围添加的表名和字段蜱的功能 - 或写适当蜱一个简单的包装,函数调用'DB-> where' (当然有'false'第三个参数)。 – uzsolt

+0

这是一个耻辱,这不是在这里的文档。 http://ellislab.com/codeigniter/user-guide/database/active_record.html – Tyguy7

+1

@ user1253085它在那里。你一定忽略了。 –