2016-09-18 9 views
0

私はCodeIgniterクエリービルダーを使用しています。私はそこにhaving句を追加したいと思います。 次のように私のコードが見えます: (私は、クエリの他の部分を省略):Codeigniter INステートメントでクエリービルダを持つクエリ

$this->db->having($filterarray); 

と私は事前にこのようfilterarrayを構築:

$filterarray = array(); 
     if (!empty($filters['interests'])) { 
      $interestids = $this->interests_model->getAllIdsByDescription($filters['interests']); 
      $filterarray['interests IN'] = $interestids; 
     } 

My機能getAllIdsByDescriptionは次のようになります。

function getAllIdsByDescription($names){ 
    $res = "("; 

    $query = $this->db->where_in('description', $names) 
     ->select('interest_id') 
     ->get($this->tableName); 
    foreach ($query->result() as $interestid) { 
     $res .= $interestid->interest_id; 
     $res .= ", "; 
    } 
    $res = substr($res, 0, -2); 
    $res .= ")"; 
    return $res; 
} 

私のクエリは次のように解釈されるため、エラーが発生します:

HAVING interests IN '(7)' 

(7)の周りの引用符を削除するにはどうすればよいですか?

+0

'' '(7)' への関心を持っていることから

。クエリはIDの**リスト**のためのもので、コードで指定したものです。どのような「エラー」(または予期しない動作)が実際に表示されていますか? –

+0

問題は '(7)'の周りの引用符でした。 PaulDの答えをFALSEで使って解決しました。 – Dennis

答えて

0

エスケープを無効にすることができます。

If you are using a database that CodeIgniter escapes queries for, you can prevent escaping content by passing an optional third argument, and setting it to FALSE.

$this->db->having('user_id', 45); // Produces: HAVING `user_id` = 45 
$this->db->having('user_id', 45, FALSE); // Produces: HAVING user_id = 45 

ただ一つの引数として単一の配列を渡すときにこれを実装していますが、追加のFALSEパラメータを必要とするかどうかはわかりません。それはエラーではありません - http://www.codeigniter.com/user_guide/database/query_builder.html

関連する問題