PHP-PDO-MySQL-LIKE和prepared语句
假设PHP-PDO-MySQL-LIKE和prepared语句,php,mysql,pdo,Php,Mysql,Pdo,假设$search是来自用户输入的字符串,$db是有效的PDO引用 据我所知,以下模块是首选模块,应该可以工作: $imageStatement = $db->prepare("SELECT images.whatever FROM images WHERE images.title LIKE :titleSearch OR images.description LIKE :descriptionSearch"); $imageStatement->bindValue(':title
$search
是来自用户输入的字符串,$db
是有效的PDO引用
据我所知,以下模块是首选模块,应该可以工作:
$imageStatement = $db->prepare("SELECT
images.whatever
FROM images
WHERE images.title LIKE :titleSearch OR images.description LIKE :descriptionSearch");
$imageStatement->bindValue(':titleSearch', "%{$search}%");
$imageStatement->bindValue(':descriptionSearch', "%{$search}%");
$images = $imageStatement->fetchAll();
它返回0个结果,而以下返回预期回报:
$search = $db->quote("%{$search}%");
$images = $db->query("SELECT
images.whatever
FROM images
WHERE images.title LIKE {$search} OR images.description LIKE {$search}")->fetchAll();
我做错了什么?您从未执行过PDO语句。绑定参数后,在检索结果之前调用
execute
$imageStatement->bindValue(':titleSearch', "%{$search}%");
$imageStatement->bindValue(':descriptionSearch', "%{$search}%");
$imageStatement->execute(); //ADD THIS STATEMENT
$images = $imageStatement->fetchAll();
PDO的
query
函数不要求您调用execute
,因为它不是参数化查询。您从未执行过PDO语句。绑定参数后,在检索结果之前调用execute
$imageStatement->bindValue(':titleSearch', "%{$search}%");
$imageStatement->bindValue(':descriptionSearch', "%{$search}%");
$imageStatement->execute(); //ADD THIS STATEMENT
$images = $imageStatement->fetchAll();
PDO的
query
函数不要求您调用execute
,因为它不是一个参数化查询。yikes-我是个白痴-只是忘了执行-我会尽快接受(6分钟)。Thanks@BigMoMo没问题。我们都会犯这样的错误。哎呀,我是个白痴,只是忘了执行,我会尽快接受(6分钟)。Thanks@BigMoMo没问题。我们都会犯这样的错误。