Unix Technical Forum

Request problem (with \\)

This is a discussion on Request problem (with \\) within the MySQL General forum forums, part of the MySQL category; --> Hello list, I am currently trying to fix a bug in a search function with a request like this ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:38 AM
Gabriel Linder
 
Posts: n/a
Default Request problem (with \\)

Hello list,

I am currently trying to fix a bug in a search function with a request
like this one :
select * from forum where topic like '%[...]%' ;

where [...] is a string escaped by mysql_real_escape_string (C API) and
topic is a varchar field (not null).

It works, but there is a bug if someone is searching the character
backslash only ('\'), so the request become :
select * from forum where topic like '%\\%' ;

and it returns only topics who have a '%' in them, not a '\'. It is the
same result as if I were doing :
select * from forum where topic like '%\%' ;

To get the topics with a '\' (but it returns only the topics that ends
with a '\'), I must do :
select * from forum where topic like '%\\' ;

So it seems to me that the ending % is escaped even with '\\'. Is this a
normal behaviour ? Or am I missing something ?

Here are some infos about the server version, might be useful :
version = 4.0.20-standard
version_comment = Official MySQL-standard binary
version_compile_os = linux
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 05:38 AM
ViSolve DB Team
 
Posts: n/a
Default Re: Request problem (with \\)

Hi Gabriel,

Try as:
mysql > select * from forum where topoc like "%\\\\%";

To search for '\', specify it as '\\\\'; this is because the backslashes are
stripped once by the parser and again when the pattern match is made,
leaving a single backslash to be matched against. (Exception: At the end of
the pattern string, backslash can be specified as '\\'. At the end of the
string, backslash stands for itself because there is nothing following to
escape.)

Ref: http://dev.mysql.com/doc/refman/5.1/...functions.html

Thanks
ViSolve DB Team.

----- Original Message -----
From: "Gabriel Linder" <linder@jeuxvideo.com>
To: "MySQL List" <mysql@lists.mysql.com>
Sent: Friday, January 19, 2007 9:43 PM
Subject: Request problem (with \\)


> Hello list,
>
> I am currently trying to fix a bug in a search function with a request
> like this one :
> select * from forum where topic like '%[...]%' ;
>
> where [...] is a string escaped by mysql_real_escape_string (C API) and
> topic is a varchar field (not null).
>
> It works, but there is a bug if someone is searching the character
> backslash only ('\'), so the request become :
> select * from forum where topic like '%\\%' ;
>
> and it returns only topics who have a '%' in them, not a '\'. It is the
> same result as if I were doing :
> select * from forum where topic like '%\%' ;
>
> To get the topics with a '\' (but it returns only the topics that ends
> with a '\'), I must do :
> select * from forum where topic like '%\\' ;
>
> So it seems to me that the ending % is escaped even with '\\'. Is this a
> normal behaviour ? Or am I missing something ?
>
> Here are some infos about the server version, might be useful :
> version = 4.0.20-standard
> version_comment = Official MySQL-standard binary
> version_compile_os = linux
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=m...rt@visolve.com
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 05:38 AM
Gabriel Linder
 
Posts: n/a
Default Re: Request problem (with \\)

Hi,

It works, thanks you for your help :-)



ViSolve DB Team wrote:
> Hi Gabriel,
>
> Try as:
> mysql > select * from forum where topoc like "%\\\\%";
>
> To search for '\', specify it as '\\\\'; this is because the
> backslashes are
> stripped once by the parser and again when the pattern match is made,
> leaving a single backslash to be matched against. (Exception: At the
> end of
> the pattern string, backslash can be specified as '\\'. At the end of the
> string, backslash stands for itself because there is nothing following to
> escape.)
>
> Ref:
> http://dev.mysql.com/doc/refman/5.1/...functions.html
>
> Thanks
> ViSolve DB Team.
>
> ----- Original Message ----- From: "Gabriel Linder"
> <linder@jeuxvideo.com>
> To: "MySQL List" <mysql@lists.mysql.com>
> Sent: Friday, January 19, 2007 9:43 PM
> Subject: Request problem (with \\)
>
>
>> Hello list,
>>
>> I am currently trying to fix a bug in a search function with a
>> request like this one :
>> select * from forum where topic like '%[...]%' ;
>>
>> where [...] is a string escaped by mysql_real_escape_string (C API)
>> and topic is a varchar field (not null).
>>
>> It works, but there is a bug if someone is searching the character
>> backslash only ('\'), so the request become :
>> select * from forum where topic like '%\\%' ;
>>
>> and it returns only topics who have a '%' in them, not a '\'. It is
>> the same result as if I were doing :
>> select * from forum where topic like '%\%' ;
>>
>> To get the topics with a '\' (but it returns only the topics that
>> ends with a '\'), I must do :
>> select * from forum where topic like '%\\' ;
>>
>> So it seems to me that the ending % is escaped even with '\\'. Is
>> this a normal behaviour ? Or am I missing something ?
>>
>> Here are some infos about the server version, might be useful :
>> version = 4.0.20-standard
>> version_comment = Official MySQL-standard binary
>> version_compile_os = linux
>>
>> --
>> MySQL General Mailing List
>> For list archives: http://lists.mysql.com/mysql
>> To unsubscribe:
>> http://lists.mysql.com/mysql?unsub=m...rt@visolve.com
>>

>
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:27 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com