tags:

views:

51

answers:

3

i have a table (tbl_world) which look like this

  id | first_name | last_name | age | class |

now i want to search the text which can be anywhere in first_name or in last_name

i m using below mysql query

 "SELECT * FROM tbl_world WHERE REGEXP '".$word."' IN( first_name, last_name)";

where $word is user input (means if i search 'hell' then 'hello' as well as 'wellhell' also returned in result)

above query display error, please suggest me optimize method for search in mysql.

addition question: should i use LIKR or RLIKE?

A: 
 "SELECT * FROM tbl_world 
WHERE first_name LIKE  '%".$word."%' 
OR last_name LIKE  '%".$word."%'";

(Consider enabling fulltext search if you want to search for a string among multiple fields.)

LesterDove
+2  A: 

I would do it with:

"SELECT * FROM tbl_world WHERE first_name REGEXP '" . $word .
    "' OR  last_name REGEXP '" . $word . "'"
slebetman
A: 
"SELECT * FROM tbl_world WHERE first_name LIKE '%'".$word."%' OR last_name LIKE '%".$word."%'"
Ian Clelland