Select query with htm code ' ' in data field

Discussion in 'MySQL' started by ched, Oct 19, 2011.

  1. #1
    I have a mysql table with the following in a field 'Queen's'

    a simplified version of my code:

    $Loc = "Queen's";

    $query = 'SELECT PRODUCT_LOCATION FROM PRODUCTS WHERE (PRODUCT_LOCATION LIKE \"%$Loc%\")';

    $result = mysql_query($query);

    The problem is that the html code within the $Loc is getting 'converted' to the display character so the query doesn't match anything.

    I can't really change the html codes within the database as others use it and I don't know what implications changing the data might cause.

    Any ideas how I can get the query to work correctly?

    Thanks for any help.
     
    ched, Oct 19, 2011 IP
  2. Divided

    Divided Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    0
    #2
    If your problem is what I think it is, look at html_entity_decode:

    http://php.net/manual/en/function.html-entity-decode.php

    Also, if it what I think it is, you need to sanitise user input before putting it straight into a query otherwise someone could inject their own sql into your query.
     
    Divided, Oct 20, 2011 IP
  3. ched

    ched Active Member

    Messages:
    185
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Cheers for that very helpful. Unfortunately I have no control over the data in the DB.
     
    ched, Oct 24, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    What's in the database, Queen's or Queen's?
     
    Rukbat, Oct 24, 2011 IP
  5. ched

    ched Active Member

    Messages:
    185
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #5
    Rukbat second one : & # 3 9 ; s

    Cheers
     
    ched, Oct 24, 2011 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Then just html-encode $Loc before you use it in your query.
     
    Rukbat, Oct 24, 2011 IP