我正在尝试将一些查询转换为PDO,并且特别是在WAMP服务器上运行时遇到一个查询的困难。如果我按照以下方式构造行,我能够将参数作为字符串“强制”,我能够使查询正常工作,但缺点是它没有绑定,我必须在同一个PHP文件中查询:
AND B.LongName LIKE '" . $searchPattern . "'
未绑定的查询设置如下:
$query_NotWorking = "SELECT
A.Description AS 'Description',
A.LongName AS 'LongName',
A.thisID AS 'This_ID',
B.LongName AS 'TargetName',
B.TargetID AS 'Target_ID',
B.NodeNumber AS 'Node',
FROM Sources A
LEFT JOIN Targets B
ON A.thisID = B.DestID
AND B.LongName LIKE :SearchPattern
WHERE B.LongName IS NOT NULL
ORDER BY 'LongName', 'TargetName';";
调用查询的页面中的PHP代码如下:
$searchPattern = "%Pattern%";
$stmt_Copy1 = $dbh_Pri -> prepare($query_NotWorking);
$stmt_Copy2 = $dbh_Sec -> prepare($query_NotWorking);
$stmt_Copy1 -> bindParam(':SearchPattern', $searchPattern, PDO::PARAM_STR, 15);
$stmt_Copy1 -> execute();
$stmt_Copy2 -> bindParam(':SearchPattern', $searchPattern, PDO::PARAM_STR, 15);
$stmt_Copy2 -> execute();