Re: store backslash in mysql database [message #170642 is a reply to message #170641] |
Fri, 12 November 2010 22:36 |
Peter
Messages: 15 Registered: March 2003
Karma:
|
Junior Member |
|
|
In article <ibke2p$9gj$1(at)news(dot)eternal-september(dot)org>,
jstucklex(at)attglobal(dot)net says...
> On 11/12/2010 5:03 PM, 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.
>>
>
> Yes, this would be incorrect because the "\" is interpreted as the start
> of a string (") followed by an escaped quote (/"). The end of the
> string would be the quote at the beginning of what you think should be
> the second string.
>
> You need to do str_replace("\\", "\\\\", $string);
>
> Better would be to fix your encryption routine to not generate a single
> backslash.
>
>
Cheers Jerry. Had grabbed the routine online so wasn't aware that it was
doing this. Just accepted that it was what I needed to get the job done.
Why re-invent the wheel, as they say.
However, will examine the routine to see if I can figure out how to
avoid creating the backslash. Problem is though, that the same
encryption routine is also used to decrypt, so my initial concern would
be that I could place some code that prevents the backslash being
created, but would it work in reverse when I'm decrypting the string.
Anyway, that's my problem, not yours.
Thanks for helping.
--
Pete Ives
Remove All_stRESS before sending me an email
|
|
|