我正在使用 item_name
列重写带有连字符的 URL。例如将 animals of theplanet-v9
替换为 http://example.com/book/animals-of-the-planet-v9
并使用 str_replace(' ' ,'-',$item_name)
。我如何回到 MySQL 是:
$item_name = str_replace('-', ' ', $_GET['item_name']);
SELECT * FROM table where item_name LIKE '% $item_name %'
“animals-of-the-planet -v9”变成“animals of the Planet v9”
网址:example.com/animals-of-the-planet-v9
上面的查询匹配 0 个结果,我注意到任何包含连字符 - 的列都不匹配,因为有时它需要一个原始连字符而不是字符串中重写的连字符。我还尝试在查询中使用 = 和 LIKE 。我如何弄清楚哪里可以有一个非常简单的 URL,其中包含连字符,但没有任何数字 id?
最佳答案
试试这个,
$item_name = $_GET['item_name'];
SELECT * FROM table where REPLACE(item_name, ' ', '-') = '$item_name'
注意 - 在使用 $item_name 之前对其进行转义。以防止您的查询受到 mysql 注入(inject)。
关于mysql - 如何将带有连字符的重写 URL 字符串与 MYSQL 中可能有 1 个连字符的字符串相匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48595384/