php - MySQL转义特殊字符

标签 php mysql escaping character sql-like

所以,我有下表:

ID  product
1   usb-stick
2   blackberry
3   cup of tea
4   usb-stick 2.0
5   computer

并且希望每当我使用 LIKE 条件查看数据时都能实现这一点,例如:

SELECT * FROM products WHERE product LIKE '% <value> %'

我会得到我正在寻找的结果。但是因为我的列中存储了特殊字符,所以我必须写下实际值才能将它们作为结果返回。

所以我的问题是,是否有任何方法可以转义特殊字符(存储在我的列中,例如:usb-stick)以便也可以搜索以下语句:

ID  product         title / keyword (seperated by comma) product must be searchable for
1   usb-stick       usb stick, usb        stick (notice the spaces)
2   blackberry      blackberry, black berry
3   cup of tea      cup of tee
4   usb-stick 2.0   usb stick 2.0, usb stick 2, usb-stick 2, etc
5   computer        computer

简而言之,我只是希望该产品也可以搜索其他语句(因此不仅仅是实际值(value)/标题)。

希望得到帮助!

最佳答案

您可以使用replace()

SELECT * FROM products WHERE replace(product , '-', '') LIKE '% <value> %'

编辑,这一点都不友好,但可以工作:

SELECT * FROM products WHERE
REPLACE(
    REPLACE(
        REPLACE(
            REPLACE(
                REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE(
                                REPLACE(
                                    REPLACE(
                                        REPLACE(
                                            REPLACE(
                                                REPLACE(
                                                    REPLACE(
                                                        REPLACE(
                                                            REPLACE(
                                                                REPLACE(
                                                                    REPLACE(
                                                                        REPLACE(
                                                                            REPLACE(
                                                                                REPLACE(
                                                                                    REPLACE(
                                                                                        REPLACE(
                                                                                            REPLACE(
                                                                                                REPLACE(
                                                                                                    REPLACE(
                                                                REPLACE(
                                                                    REPLACE(
                                                                        REPLACE(
                                                                            REPLACE(
                                                                                REPLACE(
                                                                                    REPLACE(
                                                                                        REPLACE(product, '\"', ''),
                                                                                    '.', ''),
                                                                                '?', ''),
                                                                            '`', ''),
                                                                        '<', ''),
                                                                    '=', ''),
                                                                '{', ''),
                                                                                                    '}', ''),
                                                                                                '[', ''),
                                                                                            ']', ''),
                                                                                        '|', ''),
                                                                                    '\'', ''),
                                                                                ':', ''),
                                                                            ';', ''),
                                                                        '~', ''),
                                                                    '!', ''),
                                                                '@', ''),
                                                            '#', ''),
                                                        '$', ''),
                                                    '%', ''),
                                                '^', ''),
                                            '&', ''),
                                        '*', ''),
                                    '_', ''),
                                '+', ''),
                            ',', ''),
                        '/', ''),
                    '(', ''),
                ')', ''),
            '-', ''),
        '>', ''),
    ' ', '-'),
'--', '-') like '% <value> %'

关于php - MySQL转义特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31893705/

相关文章:

php - 从类别页面 wordpress 中删除主题生成的自定义 css

Mysql INSERT/SELECT 基于Join?

mysql - 将 char_length 分配给变量时出错

php - 使用单引号时在 PHP 中创建制表符和换行符的正确方法是什么?

escaping - 为什么我必须对代码中的文件路径使用双反斜杠?

PHP/jQuery Ajax/在 Ajax 处理页面中加载 xml

php - Laravel:自定义登录重定向到/home 而不是/welcome 页面

PHP Google Api 客户端 SQL 管理员未启用身份验证 IP

php - 检查 php 中的空选择不会给出正确的结果

python - 在 Jinja 中是否有安全的 striptags 替代品?