mysql - 如何将带有连字符的重写 URL 字符串与 MYSQL 中可能有 1 个连字符的字符串相匹配?

标签 mysql hyphenation

我正在使用 item_name 列重写带有连字符的 URL。例如将 animals of theplanet-v9 替换为 http://example.com/book/animals-of-the-planet-v9 并使用 str_replace(' ' ,'-',$item_name)。我如何回到 MySQL 是:

it contains 1 hyphen in item_name

$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/

相关文章:

php - 如何从 RETS 搜索查询向表中插入数据?

javascript - 如何查看环回中第二个表中的字段?

html - 如何防止 html div 中的断字(连字符)?

android - 如何在带有 React Native Text 的 Android 中实现软连字符

html - 打破连字符而不是溢出

用于带连字符的单词的 Python 正则表达式

mysql - Bash脚本通过变量连接MySQL并查询

mysql - 相交MySql

php - PHP 中的日历下拉菜单 : keep selected value and re-use the function

css - 打散大字/防止打断布局/Hyphenator js