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


Problem :

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



Solution :

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
              }

Mysql Tutorials

Mysql Howto..

How do I query for fields containing a given text in MySQL?

How to access a MySQL database webservice over LAN? [closed]

how to mysql join for two tables

Django: How to use float precision instead of double precision in MySQL

How to check for duplicates in mysql table over multiple columns

How do I suppress the row count for tables added (AUTO_INCREMENT=N) to mysqldump output?

How to Show Top Value filed as top in Mysql

How can I update a mysql field when my user loses connection to my site?

How to use nested SELECTs in Mysql with LIKE?

How can I use ON DUPLICATE KEY UPDATE in PDO with mysql?

MySQL — using only SQL, how to return the results of an alternate query if the current query returned empty?

how to find the max of a field and group by one field in mysql

How to save an array of dates read from mysql on php?

How to surface a value for more than one row in MySQL

How to check condition before executing MYSQL query (Laravel 5)

How can I send an Arraylist of HashMaps to _POST? And then how can I dissect the array in PHP?

How to create a login form with HTML, PHP and MySQL [closed]

How to safely escape the input data in php for mysql

how to select specific value depending on a combination of subquery result in MySQL

How to concatenate a string to MySQL resultset

How to retrieve data from a polymorphic MySQL table

How to upload images into MySQL database using PHP code

How to make a pair of columns unique in mysql [duplicate]

How to deal with Apostrophe while writing into Mysql database [duplicate]

How to add mysql datasource to applicationContext.xml

How to change particular column entries in a mysql table when uploading data from csv file?

How long does it take to translate SQL Server queries to MySQL?

how can i fetch data from mysql database and display it to view using codeigniter .

How can I prevent from removing a default Row in MySql?

How to ignore text in MySQL STR_TO_DATE format string?