0

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..

Raviv g
  • 55
  • 1
  • 1
  • 8

5 Answers5

2

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
              }
Anshul Parashar
  • 3,096
  • 4
  • 30
  • 56
0

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

zzlalani
  • 22,960
  • 16
  • 44
  • 73
0

try this

$query = "SELECT * FROM users WHERE user_fname like '%".$string."%' OR user_lname like '%".$string."%' OR username like '%".$string."%' OR useremail like '%".$string."'%";
zzlalani
  • 22,960
  • 16
  • 44
  • 73
Rakesh Shewale
  • 497
  • 6
  • 22
0

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

  • Please avoid mysql_... use mysqli_ or PDO
Kalpit
  • 4,906
  • 4
  • 25
  • 43
0

try this:

$query = "SELECT * FROM users 
WHERE user_fname LIKE '%$string%' 
OR user_lname LIKE '%$string%' 
OR username LIKE '%$string%' 
OR useremail LIKE '%$string%'";
Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
mamun0024
  • 71
  • 5