Problem comparing a MySQL string with a $_POST string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wlbragg
Forum Newbie
Posts: 8
Joined: Fri Jan 25, 2008 12:40 am
Location: Kansas, US

Problem comparing a MySQL string with a $_POST string

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

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

Post 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.
wlbragg
Forum Newbie
Posts: 8
Joined: Fri Jan 25, 2008 12:40 am
Location: Kansas, US

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

Post 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!
Post Reply