RE: [PHP] Very Weird Problem Continued
| From: | Ford, Mike [LSS] | Date: | Mon, 16 Jun 2003 14:00:55 +0000 |
| Subject: | RE: [PHP] Very Weird Problem Continued | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-151657@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: James Hicks [mailto:freedomadvocate@cox.net]
> Sent: 15 June 2003 14:44
>
> The following variable needs to be escaped before it can be
> submitted as an
> MySQL query. It seems that the \0 in this string is causing
> me the problem. I
> have tried several functions in an attempt to escape this
> variable. Anybody
> got any other suggestions. The resulting string should look like this:
>
> EBCO\\030774-006\\BUBBLER VALVE
>
> $variable = "EBCO\030774-006\BUBBLER VALVE";
OK -- because this string is double-quoted, PHP will process any valid
\-sequences in it. \030 is a valid sequence representing the character with
octal code 030 which, if memory serves me correctly, is [Ctrl-^] -- so
that's why that sequence is translated into a "weird character". \B, on the
other hand, is *not* a valid sequence, and so is passed into the string
unchanged -- so what you end up with in $variable is:
EBCO[Ctrl-^]774-006\BUBBLER VALVE
Thus, $variable now irrevocably has the weird [Ctrl-^] character in it in
place of \030, and no application of addslashes() or any other escaping
mechanism is going to change that.
To get a single slash at that point into the value you put in $variable, you
can do one of two things: either escape the backslash with another
backslash:
$variable = "EBCO\\030774-006\BUBBLER VALVE";
or use single quotes, so that \ sequences are not translated:
$variable = 'EBCO\030774-006\BUBBLER VALVE';
It helps, when you want to know exactly what value you've got into a
variable, to print_r() or var_dump() it -- so to show the correctness of
what I've just said, you could do the following:
$variable = "EBCO\030774-006\BUBBLER VALVE";
var_dump($variable);
$variable = "EBCO\\030774-006\BUBBLER VALVE";
var_dump($variable);
$variable = 'EBCO\030774-006\BUBBLER VALVE';
var_dump($variable);
Because either of the ways I have shown will put the literal sequence \030,
rather than [Ctrl-^], into your variable, you can happily addslashes() them
to get the desired result.
Hope this has shed a little light on your problem.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211