2014-03-12 21 views
0

我正在使用Codeigniter 2.我試圖使用Active Record進行mysql查詢。問題是當數組傳遞給where_in爲空時出現錯誤。我問是否有什麼辦法可以忽略where_in條件,如果數組爲空where_in codeigniter錯誤,如果數組爲空

此條件例如:

$this->db->where_in('p.brand_id', $brands); 

這是我得到的錯誤:

您有一個您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到在'AND AND p附近使用的正確語法。 brand_id IN()

+0

不要害怕,只顯示你的查詢花花公子 –

+0

@echo_Me:他*是*。 '$ this-> db-> where_in('p.brand_id',$ brands);'將'WHERE IN'追加到查詢中。這個問題很清楚。 –

+0

你確定$品牌是一個數組嗎?如果不是,它會失敗...檢查這個[link](http://ellislab.com/codeigniter/user-guide/database/active_record.html) – iubema

回答

0

如果數組爲空,則不要調用where_in()

if(is_array($brands) && count($brands) > 0){ 
    $this->db->where_in('p.brand_id', $brands); 
} 
0

我的Active Record(DB_active_rec.php類)改變了,我將此代碼添加到忽略where_in條件,如果數值數組是空的。所以現在適用於我。 我添加的代碼之間// @開頭和// @結束

/** 
* Where_in 
* 
* Called by where_in, where_in_or, where_not_in, where_not_in_or 
* 
* @param string The field to search 
* @param array The values searched on 
* @param boolean If the statement would be IN or NOT IN 
* @param string 
* @return object 
*/ 
protected function _where_in($key = NULL, $values = NULL, $not = FALSE, $type = 'AND ') 
{ 
    if ($key === NULL OR $values === NULL) 
    { 
     return; 
    } 

    //@begin 
    // Ignore where_in condition if values array's empty 
    if(is_array($values) && empty($values)) 
    { 
     return $this; 
    } 
      //@end 

      // More method stuff 
相關問題