6

我正在尝试更正数据库中被双重转义的条目。我相信在使用 mysql_real_escape_string 时打开了magic_quotes。双重转义导致某些搜索结果不正确/丢失。

我已经关闭了magic_quotes,并计划搜索带有反斜杠的条目并更新它们以删除任何双重转义。麻烦的是,当我执行查询以搜索带有反斜杠的条目时,我总是没有得到任何结果。

SELECT title FROM exampletable WHERE title LIKE '%\\\\%'

我在这里推荐使用'%\\\\%':http: //dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

如果我输出每个标题,其中许多都有不需要的反斜杠,所以我知道它们在那里。我似乎无法在查询中隔离它们。

Example Data:
Old King\'s Road
Running Down A Dream
Can\'t Stop The Sun
This One's For Me

同样,只是尝试返回带有 \ 的条目。


编辑:MySQL 版本是 5.0.92-community。排序规则是 latin1_swedish_ci。字符集是 UTF-8 Unicode

%\\% 不起作用。我试过了。它在 mysql.com 上被列为不正确:

要搜索“\”,请将其指定为“\\\\”;这是因为反斜杠被解析器剥离一次,并且在进行模式匹配时再次剥离,留下一个反斜杠进行匹配。

4

4 回答 4

5

This command works for me when I'm running the command with php:

select * from exampletable where title like '%\\\\\\\\%' or title like '\\\\\\\\%' or title like '%\\\\';

The reason I include the beginning, anywhere, and end matches is to show that they are different. If you're looking for backslashes on the end, there only needs to be 4 backslashes. this is because there is nothing to escape after it. You use 8 backslashes on the other commands because they get parsed by php.

于 2012-10-31T21:55:50.977 回答
2

您的查询在 MySQL 5.5.8 中对我来说很好:

mysql> create table exampletable (
  title varchar(255)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.15 sec)

mysql> 
mysql> insert into exampletable values ('Old King\\''s Road'),
  ('Running Down A Dream'),('Can\\''t Stop The Sun'),('This One''s For Me');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> 
mysql> select * from exampletable WHERE title LIKE '%\\\\%';
+---------------------+
| title               |
+---------------------+
| Old King\'s Road    |
| Can\'t Stop The Sun |
+---------------------+
2 rows in set (0.01 sec)
于 2011-04-11T19:08:59.300 回答
0

对于 Ruby on Rails

search_value.gsub!(/\\/, "\\\\\\\\\\")
MyModel.where(["column_name ?","%#{search_value}%"])

如果您不想使用通配符,请添加它们。

search_value.gsub!(/%/, "\\%").gsub!(/_/, "\\_")
于 2014-06-27T04:48:58.720 回答
0

尝试:

SELECT title FROM exampletable WHERE title LIKE '%\\%'
于 2011-04-11T18:32:36.920 回答