MySQL Forums
Forum List  »  Newbie

Re: Correct syntax to search for backslashes "\"
Posted by: Rick James
Date: November 07, 2010 01:48PM

Why? If you are using the correct escaping and unescaping functions, you never need to do that.

OK, Let's say you have \' in the database because of excessive escaping before the INSERT.

First problem is that you have not given enough context? Perl? PHP? Other? PHP is rather aggressive at replacing \\ with \ in strings. This could lead to
where name like '%\\\'%'
actually looking like
where name like '%\'%'
to MySQL. Which is the same as "search for name with an apostrophe" (no backslash).

To help diagnose this, ...
If you are in PHP
$sql = "... where name like '%\\\'%'";
echo "<code>$sql</code>";

Hint: don't do '%\\\'%', instead do "%'%", and escape the " if necessary:
$sql = "... where name like \"%'\"%";

But, since you are looking for a backslash, maybe this is what you need:
$sql = "... where name like \"%\\'\"%";
I think that will work in both Perl and PHP.

In Perl, I would do this to minimize backslashes:
$sql = qq{... where name like "%\\'%"};

Options: ReplyQuote


Subject
Written By
Posted
Re: Correct syntax to search for backslashes "\"
November 07, 2010 01:48PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.