Storm Spirit Storm Spirit - 4 months ago 7
SQL Question

How to search on lastname and firstname with one variable given

I have first_name and last_name

so this is my current query on PHP

SELECT * FROM users WHERE CONCAT_WS(first_name, " ", last_name)') LIKE $name.'%'


my sample DB:

firstname | lastname
-------------------------
Joe | Smith
Bien | Tibor
Brocs | Mar
Joe | Gold





this is my Test Result

Test 1:
name = Joe


firstname | lastname
-------------------------
Joe | Smith
Joe | Gold


Test 2:
name = Smith


firstname | lastname
-------------------------
"No Results Found"

Answer

Use the wildcard % on either side of the $name variable.

$sql = "SELECT * FROM `users` WHERE CONCAT(`first_name`, ' ', `last_name`) LIKE '%".$name."%'";
Comments