Advanced Search Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rob21century
    New Member
    • May 2007
    • 4

    Advanced Search Query

    Hello i will be a web designer one day but until then i need a little help

    I have a search query that searches the database for user entered string. See below:

    $query = "SELECT * From MyTable WHERE Company_Name LIKE \"%$postrimmedc ompname%\" ORDER BY Company_Name" ;

    This works fine. If I enter "estate agents", I get the results. and also if I enter "estate" or I enter "agents" I also get the results.

    However, if I enter "estate agents housing", it doesnt display the results.

    I thought using the LIKE operator and the % sign would make this work. But it doesnt.

    if any out their could point me in the right direction i would be most grateful :)
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    The %..% signs tell you SQL to search for the string in between the signs anywhere inside the field value. So if you were to have a VARCHAR field containing the name 'John Doe' and you were to search for the value '%John%', it would return the field. Also, if you searched for '%John Doe%' it would return it. But if you go outside the value of the field, e.g. '%Hi John Doe%', the search string would not exist inside the field, ergo it would not return the field.

    And to further complicate the matters, if you put a % sign at either end of the search string (e.g. 'John%'), but not the other end, it will assume that the seach string is at the end of the field your are searhing. So using our field 'John Doe', if you were to seach for 'John%' it would return the field, but if you were to search for 'Doe%' it would not.

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      You will need to do an explode on your string first, using " " as a delimiter. Then build your query with LIKEs combined with ORs.

      Comment

      Working...