Whats wrong with this MYSQL statement?

Discussion in 'MySQL' started by dawilster, Jan 12, 2011.

  1. #1
    Hi,

    $query = $db->query("SELECT * FROM suburb_tlb WHERE 'suburb' LIKE '$queryString%' OR 'areacode' LIKE '$queryString%'  LIMIT 10");
    Code (markup):
    Well I'm implementing an autocomplete function on my new website and I'm having a little bit of trouble with it.

    What it does it when the user enters a suburb or area code it will search both the suburb and area code tables and return a full suburb and area code.

    But the code above doesn't seem to work so does anybody know what I'm doing wrong?

    Thanks a lot
    Dawilster
     
    dawilster, Jan 12, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Define 'doesn't work'. Nothing at all happens? Something unexpected happens? Wrong results are returned? It causes polio outbreaks in nearby schools? You get an error message somewhere? Give us something more.

    I would log/capture/find out what your actual SQL statement is that is being used. Maybe you are using the wrong variable names, maybe its inserting invalid characters. Find out what the actual statement is and try to run that through your admin panel and see what gets returned.
     
    plog, Jan 13, 2011 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    plog is right, always elaborate what exactly you are asking. You can also use mysql_error() to identify the mysql error.

    The problem in the query is single quotes. Do not use single quotes around column names or replace them with back ticks (`). e.g.
    $query = $db->query("SELECT * FROM suburb_tlb WHERE suburb LIKE '$queryString%' OR areacode LIKE '$queryString%'  LIMIT 10");
    Code (markup):
     
    mwasif, Jan 14, 2011 IP
  4. diondevelopment

    diondevelopment Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this:

    $query = $db->query("SELECT * FROM `suburb_tlb` WHERE (`suburb` LIKE '$queryString%') OR (`areacode` LIKE '$queryString%')  LIMIT 10");
    Code (markup):
    make sure to use mysql_real_escape_string() on the $queryString variable.
     
    diondevelopment, Jan 15, 2011 IP
  5. dawilster

    dawilster Active Member

    Messages:
    844
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Thanks a lot I implemented the suggested code and it works great.

    Thanks again
    Dawilster

     
    dawilster, Jan 19, 2011 IP