Page 1 of 1

Problem comparing a MySQL string with a $_POST string

Posted: Fri Jan 25, 2008 12:58 am
by wlbragg
Hope this isn't something obvious I missed.
I am trying to compare two strings using == and/or strcmp(). I have no problem until the strings I am comparing have an apostrophe in them. Both strings have been placed into separate arrays. Both strings are simple, example: who's or they're. One string is coming from a $_POST variable the other is coming from a MySQL Database.
Code looks like this:

Code: Select all

 
$aWordArray[0]=trim($_POST['a1']);
 
 .........$ws = mysql_fetch_array........
$cWordArray[0]=$ws[1];
 
if ($aWordArray[0]==$cWordArray[0]) ....
or
if (strcmp($aWordArray[0], $cWordArray[0])) ......
 
Like I said, it works fine until strings have an apostrophe in them! I printed both strings to the screen before compare and they both looked correct. Anyone know offhand what dumb thing I am missing?

Thanks for any help!

Re: Problem comparing a MySQL string with a $_POST string

Posted: Fri Jan 25, 2008 1:40 am
by Christopher
Probably one of the strings is escaped, meaning that there are backslashes before the quotes (and possibly other characters). There are addslashes() and stripslashes() functions.

Re: Problem comparing a MySQL string with a $_POST string

Posted: Fri Jan 25, 2008 2:02 am
by Kieran Huggins
I'll bet it's an encoding issue - make sure both the page that the form is on AND the database table are set to UTF-8.

Re: Problem comparing a MySQL string with a $_POST string

Posted: Fri Jan 25, 2008 2:11 am
by JAM
The encoding issue is what I thought of aswell, but then again, I myself used latin (in various forms) without these issuesbefore so I'm not sure...

I'd read up on the way Magic Quotes works aswell.

Re: Problem comparing a MySQL string with a $_POST string

Posted: Fri Jan 25, 2008 11:20 pm
by wlbragg
Thanks guys,
One of the strings was escaped. I already tried to use addslashes() but that didn't work. Don't know why I didn't try stripslashes() I guess because add didn't work. Stripslashes() did the trick. Thanks again for the help!