gpt4 book ai didi

sql - codeigniter like 子句覆盖 where 子句

转载 作者:行者123 更新时间:2023-12-04 20:46:42 35 4
gpt4 key购买 nike

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

最佳答案

您的查询应该是

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

所以这个查询将返回 所有行 其中批准 = 'y' 或其中标题,正文,作者匹配 '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' 并且标题或正文或作者或描述与“另一个”匹配的行。我相信这就是您想要实现的目标。

关于sql - codeigniter like 子句覆盖 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762017/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com