2013-05-26 14 views
5

少なくともそれは起こっているようです。私はウェブサイト用の検索バーを作成しようとしていますが、承認されたコンテンツのみを取り戻すwhere節を読んでいないことを除いて、機能します。なぜそれが問題になるのか分かります。codeigniterのような節はwhere節をオーバーライドします

とにかく、これは私が私のモデルに持っているもの

$match = $this->input->post('search');  
$this->db->where('approved', 'y'); 
$this->db->like('description', $match); 
$this->db->or_like('title', $match); 
$this->db->or_like('body', $match); 
$this->db->or_like('author', $match); 

$query = $this->db->get('story_tbl'); 

return $query->result(); 

であると私は、クエリを印刷するとき、それはWHERE句を見ているように、それはそう、私は戻ってのものを得るとき、それはある事を引っ張っています承認されていないか、レビュー中です。ここで

は私の印刷問い合わせは

SELECT * FROM (`story_tbl`) WHERE `approved` = 'y' AND `description` LIKE 
'%another%' OR `title` LIKE '%another%' OR `body` LIKE '%another%' OR 
`author` LIKE '%another%' 

答えて

2

であるあなたのクエリは、これらのブラケットをチェック

SELECT * FROM (`story_tbl`) WHERE `approved` = 'y' AND (`description` LIKE 
'%another%' OR `title` LIKE '%another%' OR `body` LIKE '%another%' OR 
`author` LIKE '%another%') 

でなければなりません。したがって、最良の選択肢は、平文$this->db->query()を使用することです。あなたがアクティブなレコードを使用して主張した場合、あなたはそれらの括弧のために、このようにそれをしなければならない -

$match = $this->input->post('search'); 
$this->db->where('approved', 'y'); 
$this->db->where("(`description` LIKE '%$match%'"); 
$this->db->or_where("`title` LIKE '%$match%'"); 
$this->db->or_where("`body` LIKE '%$match%'"); 
$this->db->or_where("`author` LIKE '%$match%')"); 
$query = $this->db->get('story_tbl'); 

EDIT:

true AND true OR true OR true //true 
false AND true OR true OR true //true just like your where clause is ignored 
false AND false OR false OR true //Still true 
false AND true OR false OR false //false 
false AND false OR false OR false //false 

だから、このクエリは=「Y承認すべての行を返します。 「ORどこタイトル、本文、著者のマッチ '他人

私の投稿クエリ

true AND (true OR true OR true) //true 
false AND (true OR true OR true) //false, where clause is checked 
true AND (false OR false OR false) //false 
true AND (true OR false OR false) //true 

承認された「y」の行を返します。また、タイトル、本文、著者または説明が「別のもの」に一致します。私はこれがあなたが達成したいと思うものだと信じています。

+0

なぜこれらの角括弧が必要ですか?これはcodeigniterがページにそれを表示する方法です。私が投稿した上記のquerryは、最初のwhere句を無視しています。私はなぜアクティブなレコードに対して$ this-> db-> querryを使う方が良いのだろうと混乱していますか? – zazvorniki

+0

ブラケットがないため、where句は無視されます。 AND、ORの仕組みを理解する必要があります。 – sakibmoon

+0

私はそれらの仕組みを理解していますが、上記のコードを貼り付けると500エラーになります。 'シンボルは違いを生むべきではありません – zazvorniki