当我在SQL查询中将两个LIKE组合在一起时,如果我在服务器上直接执行它,它可以正常工作。当我使用PDO绑定变量时,事情变得奇怪。
在服务器上,以下按预期工作
SELECT * FROM CarParkMain WHERE Make LIKE '%b%' AND Model LIKE '%02 (e10)%'
以下陈述会在我的PHP中带来预期的结果
SELECT * FROM CarParkMain WHERE Model LIKE :b
SELECT * FROM CarParkMain WHERE Make LIKE :a
SELECT * FROM CarParkMain WHERE Make LIKE '%b%' AND Model LIKE '%02 (e10)%'
以下声明不起作用(零结果)
SELECT * FROM CarParkMain WHERE Make LIKE :a AND Model LIKE :b
此语句仅返回Make LIKE的结果,表明Model LIKE不返回任何内容(与上面的模型相反,其中模型LIKE是单独的,带来结果)
SELECT * FROM CarParkMain WHERE Make LIKE :a OR Model LIKE :b
我可以使用下面的代码创建问题。有任何想法吗?我错过了一些愚蠢的东西吗?
$prepared = $pdo->prepare("SELECT * FROM CarParkMain WHERE Make LIKE :a AND Model LIKE :b");
$filter = "%b%";
$prepared->bindParam(":a", $filter);
$filter = "%02 (e10)%";
$prepared->bindParam(":b", $filter);
$prepared->execute();
$rows = $prepared->fetchAll();