Hi I have got stuck and hope someone can help me... I receive a product feed as an xml file, and with simplexml I can extract the products and assign them to variables, works OK I then use the information I have extracted in a php query on a mysql database. Problem is, some of the products contain apostrophes, and the query doesn't work for these products - presumably because the apostrophes need to be escaped. I've tried: $item=str_replace("'","\'",$item); $query="SELECT * FROM thisTable WHERE thisItem='$item'"; but that doesn't work I also tried $item=addslashes($item) $query="SELECT * FROM thisTable WHERE thisItem='$item'"; with no success I'm not very familiar with php/mysql. Am I missing something obvious? Cheers!
Try this $item=mysql_real_escape_string($item); $query="SELECT * FROM thisTable WHERE thisItem = '$item'"; Code (markup):
example item is: Cap-d'Agde so I have $item="Cap-d'Agde" $query="SELECT newPlace FROM thisTable WHERE oldPlace='$item'"; database table only has two columns - oldPlace and newPlace, I need to find the item in the first column (oldPlace) and return the item in the second column (newPlace) Adcuz, thanks but unfortunately that didn't solve it Note: the query works fine for items without apostrophes cheers
This is what i tried for you: <?php //add connection data here $oldplace = addslashes("Cap-d'Agde"); $newplace = addslashes("new with ' within"); $insert_query = "insert into tester2 (oldplace, newplace) values ('".$oldplace."','".$newplace."')"; mysql_query($insert_query); $item=addslashes("Cap-d'Agde"); $query="SELECT newplace FROM tester2 WHERE oldplace='$item'"; $result = mysql_query($query); $row = mysql_fetch_array($result); echo $row['newplace']; ?> PHP: and this printed: new with ' within Which means php works! I think your problem is with the way you stored the information in the first place. I mean, did you add the slashes when you inserted your data or not? Check it out
addslashes when you want to use in mysql queries, stripslashes when you show the information like in echo