LIKE query not working correctly

Discussion in 'MySQL' started by timallard, May 27, 2009.

  1. #1
    I have a search set up where people can search results based upon keyword, using the LIKE query.

    It works half of the time,..why?

    For instance.. if i search by keyword and search "When" I get about 30 results. When I search say, Celtics, or Botox, even though i know there are results, I get nothing?

    any ideas? IM using PHP

    Thank You,
    -Tim
     
    timallard, May 27, 2009 IP
  2. freelistfool

    freelistfool Peon

    Messages:
    1,801
    Likes Received:
    101
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Where does 'When' and 'Celtics' exist in the column string? Like with a '%' behind the text will only match words before the '%'. Like with a '%' in front and behind will find rows that have the word anywhere in the string (however this is slower since you'll have to read the entire table or index).
     
    freelistfool, May 27, 2009 IP
  3. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Can you post the database structure and SQL statement? Also, PHP is the scripting language. You are probably using MySQL on the backend, correct?
     
    Social.Network, May 27, 2009 IP
  4. JyeP

    JyeP Active Member

    Messages:
    329
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    78
    #4
    There could be a few factors causing your problem.
    But it is hard to tell without seeing thr actual code for myself.

    Be sure that you have correctly made the MYSQL (or other database query), it should look like this:

    "$sql = mysql_query("SELECT * FROM table_name WHERE column_name LIKE value%");

    while ($row = mysql_fetch_row($sql)) {
    echo "$row[0] $row[1] $row[2] <br />";
    } "

    Make sure you are using * instead of a preset value so that the search with query the entire database.

    You may also want to try adding a wild card character searches. You can do this by adding some underscores after value. The amount of underscores relates to the amount of wildcard characters. e.g: "value____"

    It may be possible that the results that are not showing contain a % character. If so try changing your end query to this:
    $sql = mysql_query("SELECT * FROM table_name WHERE column_name LIKE value\%");

    An escape slash in front of the % character.

    Let me know if anything works.
     
    JyeP, May 27, 2009 IP
  5. Ralle

    Ralle Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    Please post your code so we can properly help you. Otherwise, JyeP pretty much covered it all.
     
    Ralle, Jun 1, 2009 IP