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

5
  • 1
    use 'like %sometext%' Commented Nov 25, 2013 at 6:44
  • like %% in all rows like i did ? Commented Nov 25, 2013 at 6:45
  • have a look at this stackoverflow.com/a/20186289/829533 Commented Nov 25, 2013 at 6:47
  • also you must know that mysql_* functions are depreciated stackoverflow.com/q/12859942/829533 Commented Nov 25, 2013 at 6:48
  • i using mysqli prepared , here i used normal mysql for test its more easer Commented Nov 25, 2013 at 6:53

5 Answers 5

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
              }
Sign up to request clarification or add additional context in comments.

4 Comments

i wil test it , 1 moment.
in case of user123 this query will search for user1234 and Auser123 too
yea this what i need , thanks i just have problem with utf8 when i put text in Hebrew , i dont get any result and say wrong all time. i will flag your post as accepted soon as the system will give me,i need wait few minutes.
@zzlalani according to user need its work because its used ORoperator here
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."'%";

Comments

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

Comments

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

Comments

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%'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.