[英]Using LIKE Wildcard is causing random characters
我有一個查詢這樣的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;
似乎字符%
引起了問題。 我嘗試進行轉義,但是必須有一些我不知道的特殊轉義或模式。 我可以幫忙。 謝謝!
您可以像這樣用大括號將變量名前后的{
和}
括起來。
$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;";
何時使用花括號:
當您在字符串中定義變量時,如果使用簡單的語法定義變量,PHP可能會將變量與其他字符混合使用,這會產生錯誤
查看更多: 何時將花括號括在變量中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.