此问题在此处已有答案:
How do I create a PDO parameterized query with a LIKE statement?(9个答案)
3小时前关门。
有没有办法
$term = $_GET['p'];
$stmt = $dbh->prepare("
SELECT *
FROM posts
WHERE heading LIKE '%$term%'
OR full_text LIKE '%$term%'
LIMIT 0 , 30
");
$stmt->execute();
$answer = $stmt->fetch(PDO::FETCH_ASSOC);
转换成类似于
$term = "'%" . $_GET['p'] . "%'";
$stmt = $dbh->prepare("
SELECT *
FROM posts
WHERE heading LIKE :term
OR full_text LIKE :term
LIMIT 0 , 30
");
$stmt->bindParam(":term", $term);
$stmt->execute();
$answer = $stmt->fetch(PDO::FETCH_ASSOC);
以便在查询中使用bindParam(":term", $term);
而不是'%$term%'
?
我已经看过这些Using LIKE in bindParam for a MySQL PDO Query和Mysql, PDO - Like statement not working using bindParam,但是它们没有给予我任何正确的答案。
2条答案
按热度按时间dced5bon1#
将通配符连接到SQL中的变量:
sczxawaw2#
您只需要%。不要使用'(简单的queto)
例如: