MySQL Search - WHERE LIKE

Discussion in 'MySQL' started by Marlorn, Apr 30, 2007.

  1. #1
    Hey all,

    I'm making a Flash AMFPHP app that has search functionality, but I'm not quite sure what the PHP search query should look like. This is what I have at the moment and is working:

    function doSearch($input) {
    
    //Create SQL query
    $sql = "SELECT * FROM nodes WHERE description LIKE ('%$input%')";
    
    //Run query on database
    $result = mysql_query($sql);
    
    //Return result
    return $result;
    Code (markup):
    But I want to search more than just the description field. I've tried the following, but it breaks:

    function doSearch($input) {
    
    //Create SQL query
    $sql = "SELECT * FROM nodes WHERE (name, description, etc) LIKE ('%$input%')";
    
    //Run query on database
    $result = mysql_query($sql);
    
    //Return result
    return $result;
    Code (markup):
    Any suggestions? Am I overlooking some quotes somewhere? I've also been looking into the WHERE [arg] AGAINST [arg] method without any success either.
     
    Marlorn, Apr 30, 2007 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Try something like this:

    SELECT * FROM nodes WHERE name LIKE '%$input%' OR description LIKE '%$input%'
    Code (markup):
     
    SoKickIt, Apr 30, 2007 IP
    Marlorn likes this.
  3. Webice

    Webice Peon

    Messages:
    49
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You should try this also, if you make a fulltext index on name and description.

    SELECT * FROM nodes WHERE MATCH (name,description) AGAINST ('%$input%')
    Code (markup):
    For more info:
    Mysql full-text search
     
    Webice, Apr 30, 2007 IP
    Marlorn likes this.
  4. Marlorn

    Marlorn Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I tried both of those suggestions including some variations on them without any luck :( That would leave me to believe it's something I'm overlooking with regards to the db structure. Here's a screenie of my db:

    [​IMG]
     
    Marlorn, Apr 30, 2007 IP
  5. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Can you post the exact query you're trying to execute?
     
    SoKickIt, Apr 30, 2007 IP
  6. Marlorn

    Marlorn Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Actually, I just got it to work using:

    $sql = "SELECT * FROM nodes WHERE name LIKE ('%$input%') OR description LIKE ('%$input%') OR phone LIKE ('%$input%') OR url LIKE ('%$input%') OR hours LIKE ('%$input%') OR address LIKE ('%$input%') OR email LIKE ('%$input%')";
    Code (markup):
    I could have sworn I tried this exact thing earlier and it wasn't working. Thanks for the help!
     
    Marlorn, Apr 30, 2007 IP
  7. RaginBajin

    RaginBajin Peon

    Messages:
    87
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I wouldn't do it that way at all.

    On your application, I would check to see if any values have been entered in on that peice of the form and only include them into the query if they are necessary. Doing what everyone is suggesting is going to create a very very very slow query becasue % % on both sides of a like statement does not use indexes.

    So, it will be like this:

    
    $sql = "SELECT * FROM nodes where";
    
    if ($post['name'] != ""){
       $sql .= "name like '%STUFF%';
    }
    elseif($post['description'] != "") {
       $sql .= "and description like '%STUFF%'";
    }
    
    
    Code (markup):
    and the if statement would just keep going on and on for the columns you want.. The if statements are quick so it won't take you much time to check them.
     
    RaginBajin, May 1, 2007 IP
  8. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #8
    But it's obvious that he wants to search for the same term in all fields.
     
    SoKickIt, May 1, 2007 IP
  9. Marlorn

    Marlorn Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I appreciate the concern RaginBajin, but truth be told, I'm only searching about 15-20 entries, so I'm not too concerned about query times. Besides, I'm just looking for a quick fix (for now) :D

    And you're right, SoKickIt, I am wanting to search for the same term in any of the fields :)
     
    Marlorn, May 1, 2007 IP