php query problem with apostrophe

Discussion in 'PHP' started by Rasputin, Jun 21, 2009.

  1. #1
    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!
     
    Rasputin, Jun 21, 2009 IP
  2. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    Post actual field data. such as your item name in database
     
    KingCobra, Jun 21, 2009 IP
  3. Adcuz

    Adcuz Peon

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this
    
    $item=mysql_real_escape_string($item);
    $query="SELECT * FROM thisTable WHERE thisItem = '$item'";
    
    Code (markup):
     
    Adcuz, Jun 21, 2009 IP
  4. Rasputin

    Rasputin Peon

    Messages:
    1,511
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    Rasputin, Jun 21, 2009 IP
  5. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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
     
    zeronese, Jun 21, 2009 IP
  6. amber.long83

    amber.long83 Active Member

    Messages:
    144
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #6
    you need to use stripslashes function rather then addslashes
     
    amber.long83, Jun 22, 2009 IP
  7. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    addslashes when you want to use in mysql queries, stripslashes when you show the information like in echo ;)
     
    zeronese, Jun 22, 2009 IP