【发布时间】:2018-08-27 05:42:36
【问题描述】:
我有一个带有如下查询的 php 脚本:
$b = $a[0]; // 2115
$c = $a[1]; // 2197
$sql = "SELECT DISTINCT a.document_id
FROM archive_locations a
INNER JOIN archive_locations b
ON (a.document_id = b.document_id)
AND
(
a.street_address LIKE '%$b%'
OR a.location_name LIKE '%$b%'
)
AND
(
b.street_address LIKE '%$c%'
OR b.location_name LIKE '%$c%'
)
ORDER BY a.document_id;";
但是当我回显这个查询时,我得到了这个:
SELECT DISTINCT a.document_id
FROM archive_locations a
INNER JOIN archive_locations b
ON (a.document_id = b.document_id)
AND
(
a.street_address LIKE '%!15%'
OR a.location_name LIKE '%!15%'
)
AND
(
b.street_address LIKE '!97%'
OR b.location_name LIKE '!97%'
)
ORDER BY a.document_id;
似乎是字符% 导致了这个问题。我尝试转义它,但一定有一些我不知道的特殊转义或模式需要。我可以使用一些帮助。谢谢!
【问题讨论】:
-
使用参数。不要使用查询字符串。
-
@Gordon Linoff - 请举例。
标签: php postgresql wildcard sql-like