Raviv g
Raviv g

Reputation: 65

How to find similar results from all rows in DB with MySQL query

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

Answers (5)

mamun0024
mamun0024

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

zzlalani
zzlalani

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

Kalpit
Kalpit

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

  • Please avoid mysql_... use mysqli_ or PDO

Upvotes: 0

Rakesh Shewale
Rakesh Shewale

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

Anshul Parashar
Anshul Parashar

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

Related Questions