Reputation: 65
I would like to make "Search you'r LOGIN" like in facebook :
Eg. searching for "Stock Overflow" would return
Stack Overflow
SharePoint Overflow
Math Overflow
Politic Overflow
VFX Overflow
Eg. searching for "LO" would return:
pabLO picasso
michelangeLO
jackson polLOck
Eg. searching for username "user123" would return :
user123
user1234
and etc ...
My Database rows :
userid | username | useremail | user_fname | user_lname
I would like to make a search input that search the word in any of this rows like above examples,
Here my php till now :
$string = $purifier->purify(@$_POST['string']);
$query = "SELECT * FROM users WHERE user_fname = '".$string."' OR user_lname = '".$string."' OR username = '".$string."' OR useremail= '".$string."'";
mysql_query("SET NAMES 'utf8'");
$result2 = mysql_query($query);
$num = mysql_num_rows($result2);
if($num == 1)
{
//true
}else{
//not found nothing
}
this way is not working good , and its not return all the similar reuslts of the word that i put in search input. plus how i return it with foreach if there is more then 1 similar result ?
Thanks allot.
Update :
Thanks all , my updated code to fix it :
$query = "SELECT * FROM users WHERE user_fname like '%".$string."%' OR user_lname like '%".$string."%' OR username like '%".$string."%' OR useremail like '%".$string."%'";
and i am not using mysql , just for the examples i had more easy to do like this..
Upvotes: 0
Views: 277
Reputation: 71
try this:
$query = "SELECT * FROM users
WHERE user_fname LIKE '%$string%'
OR user_lname LIKE '%$string%'
OR username LIKE '%$string%'
OR useremail LIKE '%$string%'";
Upvotes: 0
Reputation: 24354
Change your query with this
$query = "SELECT * FROM users WHERE user_fname LIKE '".$string."%' OR user_lname LIKE '".$string."%' OR username LIKE '".$string."%' OR useremail LIKE '".$string."%'";
NOTE:
If you have user123
keyword and you want to make a search for all the rows that have data user123*
you can apply the wildcard $string%
And in case of *user123*
you can use %$string%
And in case of *user123
you can use %$string
Upvotes: 0
Reputation: 4936
try this way
$query = "SELECT * FROM users WHERE user_fname LIKE '%".$string."%' OR user_lname = '%".$string."%' OR username = '%".$string."%' OR useremail= '%".$string."%'";
for info for Like keyword
Note
Upvotes: 0
Reputation: 507
try this
$query = "SELECT * FROM users WHERE user_fname like '%".$string."%' OR user_lname like '%".$string."%' OR username like '%".$string."%' OR useremail like '%".$string."'%";
Upvotes: 0
Reputation: 3126
try this
$string = $purifier->purify(@$_POST['string']);
$query = "SELECT * FROM users WHERE user_fname like '%".$string."%' OR user_lname like '%".$string."%' OR username like '%".$string."%' OR useremail like '%".$string."%'";
mysql_query("SET NAMES 'utf8'");
$result2 = mysql_query($query);
$num = mysql_num_rows($result2);
if($num == 1)
{
//true
}else{
//not found nothing
}
Upvotes: 2