2014-04-18 101 views
0

我在我的模型實現了一個簡單的查詢:笨查詢數據庫錯誤

$condition = "id='$user_id' AND (role='admin' OR role='manager' OR role='staff')" ; 
$this->db->select("password,ref_id,role"); 
$this->db->from("user"); 
$this->db->where($condition); 
$query = $this->db->get(); 
return $query->row_array(); 

然而,發生瞭如下錯誤:

Error Number: 1054 

Unknown column 'id='1'' in 'where clause' 

SELECT `password`, `ref_id`, `role` FROM (`user`) WHERE `id='1'` AND (role='customer' OR role='supplier') 

Filename: D:\MYSERVER\wamp\www\system\database\DB_driver.php 

Line Number: 330 

那麼,什麼是錯的?

回答

0

你可以試試這個:

$this->db->where($condition, NULL, false); 

在上面的語法中,第一種是爲condition,第二個參數是value這是NULL這裏,和第三個是用於轉義字符串(默認爲true)。

+0

你能否解釋第二和第三參數的含義? – user3453318

+0

第一個用於名稱,第二個參數用於值,第三個用於轉義字符串(默認爲true)。 –

+0

看看'id ='1'' CI是如何逃避字符串的,這就是爲什麼這個錯誤正在發生。 –

0
$this->db->select("password,ref_id,role"); 
$this->db->from("user"); 
$this->db->where('id', $user_id); 
$this->db->where('role', 'manager'); 
$this->db->where_or('role', 'staff'); 

在你的代碼的情況下OR,則在那裏將無法工作。如果你在哪裏添加一個sql語句,你還必須傳遞其他參數。嘗試下面 -

$this->db->where($condition, NULL, FALSE);