Re: store backslash in mysql database [message #170677 is a reply to message #170651] |
Sun, 14 November 2010 19:08 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 11/13/2010 3:41 AM, Helmut Chang wrote:
> Am 13.11.2010 04:28, schrieb Jerry Stuckle:
>
>> mysql_real_escape_string() won't change backslashes.
>
> Of course it does. It's exactly, what this function is for:
>
> <http://www.php.net/manual/en/function.mysql-real-escape-string.php>
>
> | mysql_real_escape_string() calls MySQL's library function
> | mysql_real_escape_string, which prepends backslashes to the following
> | characters: \x00, \n, \r, \, ', " and \x1a.
>
>> It has no way to
>> know if, for instance, "\n" is a newline character or a backslash and an
>> n.
>
> Yes, it has. Because PHP has this ability:
>
> '\n' is a string, containing a backslash and an n
> "\n" is a string, containing only a newline character
>
> <?php
> $db = mysql_connect('localhost', ..., ...);
>
> $data = 'CÊ=3Fa=3F³Ýöâ8T\TBÆ';
> var_dump(mysql_real_escape_string($data, $db));
>
> $data = "Foo\nBar";
> var_dump(mysql_real_escape_string($data, $db));
>
> $data = 'Foo\nBar';
> var_dump(mysql_real_escape_string($data, $db));
> ?>
>
> Helmut
>
You're correct on the specific case of "\n" and I am incorrect. However,
please read more closely. It does NOT handle every other instance of a
backslash - it doesn't, for instance, handle \t or \b, both of which are
valid control characters. In fact, it only handles 5 specific cases.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|