【发布时间】:2011-05-18 07:07:59
【问题描述】:
我正在尝试构建这个准备好的语句:
$sql = "SELECT * FROM entries WHERE";
$sql .= " title LIKE ? OR title LIKE ? OR title LIKE ? OR title LIKE ?";
$sql .= " OR content LIKE ? OR content LIKE ? OR content LIKE ? OR content LIKE ?";
$q = $this->db->prepare($sql);
$one = $this->term;
$two = "%" . $this->term;
$three = $this->term . "%";
$four = "%" . $this->term . "%";
$q->bind_param("ssssssss", $one, $two, $three, $four, $one, $two, $three, $four);
$q->execute();
$this->db 是我的 mysqli 连接对象
$this->term 是用户提供的字符串。
我没有收到来自数据库的任何类型的错误。我知道 sql 语句有效,我用 $this->db->query() 运行它,结果很好。当我使用准备好的语句运行它时,它返回时没有结果。有什么建议吗?
【问题讨论】:
-
为什么所有的测试都在寻找“sth”、“%sth”、“sth%”或“%sth%”?这将产生与仅查找“%sth%”相同的结果,因此您可以将查询保留为
SELECT * FROM entries WHERE title LIKE '%sth%' OR content LIKE '%sth%' -
Carlos:感谢您指出这一点。我查看了有关通配符搜索的更多文档,它确实匹配任何内容(包括零匹配)。谢谢。
标签: php mysql prepared-statement