Re: store backslash in mysql database [message #170652 is a reply to message #170640] |
Sat, 13 November 2010 09:45 |
Robert Hairgrove
Messages: 19 Registered: September 2010
Karma:
|
Junior Member |
|
|
Peter wrote:
> Let me apologise in advance as I'm unsure if this can be resolved with
> php or I need to post to a SQL group.
>
> Anyway, I have some data that is encrypted before being stored, but I
> noticed that sometimes the retrieved data, when decrypted, didn't match
> the original data.
>
> What I discovered was that the encryption routine sometimes created the
> backslash character as part of the encryption string. Something like:
>
> CÊ=3Fa=3F³Ýöâ8T\TBÆ
>
> However, when this was stored in the database, the backslash became
> removed leaving:
>
> CÊ=3Fa=3F³Ýöâ8TTBÆ
>
> I thought, if I just add:
>
> $string = str_replace("\","\\",$string);
>
> that would resolve the problem. Thinking that an escaped backslash would
> allow the single backslash char to be stored.
>
> However, that appears to be syntatically incorrect with php.
>
> So is there any other way to force a backslash into the database, by
> creating an escaped backslash, or am I going completely wrong and it
> wouldn't have worked anyway and I need to deal with this issue on a
> purely SQL basis.
>
I suggest you convert the data to something like base64 format or URI
encoding before storing it in the database. That way, you avoid this
issue and perhaps a few others as well (i.e. any unforeseen implicit
conversions of binary data). Besides, you can store the data as a normal
VARCHAR or TEXT field.
PHP provides the functions base64_encode and base64_decode, for example.
|
|
|