Search query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • latika Somaya
    New Member
    • May 2007
    • 2

    Search query

    I have 2 fields say fname and lname and the query str contains full name say
    "Madhuri Dixit" how do improve the following code.

    $key=$keyword;
    $keyword = "%".$keyword."% ";

    $sql = "Select * from tbl_orders where fname LIKE '$keyword' or lname LIKE '$keyword' order by id desc";
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    #2
    Hi

    Not sure exactly what you're looking for.

    Have you tried using fulltext search? http://www.google.com/search?hl=en&q...=Google+Search

    You could split up the query string into individual words using explode and then you can specifiy the select statement to use the first word to query first names and the last to query last names...

    [CODE=php]
    $keyword=explod e(' ', $keyword);
    $sql= "SELECT fname, lname FROM table WHERE fname LIKE '{$keyword[0]}' AND lname LIKE '{$keyword[1]}'";
    [/CODE]

    Then

    You could use SQL to combine the fname and lname:

    [CODE=sql]
    SELECT CONCAT(fname,' ',lname) as name FROM table WHERE name LIKE ('%$keyword%');
    [/CODE]

    Or perhaps you should combine these two so that you have the more accurage method (first) but a fallback in case that finds nothing (second).

    Obviously you would need to put in more code to account for only one word being queried, combining the two queries etc.

    Hopefully it helps ;)
    Last edited by henryrhenryr; Jul 20 '07, 07:13 AM. Reason: typo

    Comment

    • latika Somaya
      New Member
      • May 2007
      • 2

      #3
      Hi henry

      Thanks, It worked :)

      Comment

      Working...