2011-09-25 34 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它在那裏。你一定忽略了。 –