【问题标题】:codeigniter like clause overriding where clausecodeigniter like 子句覆盖 where 子句
【发布时间】:2013-05-26 17:48:22
【问题描述】:

至少这似乎是正在发生的事情。我正在尝试为网站创建一个搜索栏,它确实有效,但它没有读取只会撤回已批准内容的 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%'

【问题讨论】:

    标签: sql codeigniter where-clause


    【解决方案1】:

    您的查询应该是

    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');
    

    编辑:

    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
    

    因此,此查询将返回 所有行 其中approved = 'y' OR where title, body, author 匹配'another'

    在我发布的查询中

    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' 并且标题或正文或作者或描述与“另一个”匹配的行。我相信这就是您想要实现的目标。

    【讨论】:

    • 我为什么要那些括号?这就是 codeigniter 将它打印到页面的方式,而我发布的上面的查询仍然忽略了我的第一个 where 子句。我很困惑,为什么在活动记录上使用 $this->db->查询会更好?
    • where 子句因为没有括号而被忽略。您需要了解 AND、OR 的工作原理。
    • 我确实了解它们是如何工作的,但是当我粘贴上面的代码时,它给了我一个 500 错误。 ` 符号不应该有所作为
    • 即使我将上述查询输入到我的 sql 编辑器中,它仍然会忽略第一个 where 子句。
    • 这不应该发生。我检查了 SQL 中的代码,它没有忽略第一个 where 子句。
    【解决方案2】:

    您可以为此使用 codeigniters 的 group_start() 和 group_end()。代码可以这样修改

    $match = $this->input->post('search');      
    $this->db->where('approved', 'y');
    
    $this->db->group_start(); //start group
    $this->db->like('description', $match);
    $this->db->or_like('title', $match);
    $this->db->or_like('body', $match);
    $this->db->or_like('author', $match);
    $this->db->group_end(); //close group
    
    $query = $this->db->get('story_tbl');
    
    return $query->result(); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-23
      • 2013-06-27
      • 2022-01-08
      • 2013-06-30
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      相关资源
      最近更新 更多