http://htmlweb.ru/ajax/example/search.php did everything exactly as on the site. How to make it look for by name and surname? I did something similar, but so far I’m looking only for OR by name OR only by last name I need to search by name and last name at the same time (Vasya Pupkin and Pupkin Vasya) here are my sketches:

<? include('auth/bd.php'); $searchq = $_GET['name']; $getName = mysql_query('SELECT * FROM users WHERE firstname LIKE "%'.addslashes($searchq).'%" OR lastname LIKE "%'.addslashes($searchq).'%"'); if(!empty($searchq)) while ($row = mysql_fetch_array($getName)) echo $row['firstname']." ".$row['lastname'] . '<br/>'; ?> 

  • OR Replace with AND . - Visman
  • @Visman Didn't Help - BedOmar
  • And why are you interested in the same variable compared with the name and surname? - Visman
  • @Visman and how then? - BedOmar
  • @BedOmar $ searchq does your full_name (Vasya Pupkin) store, or is one of them? - Vanya Avchyan

3 answers 3

We break a line on 2 and we look for in tables

 <? include('auth/bd.php'); $searchq = mb_strtolower($_GET['name']); $nameArr = explode(" ", $searchq);//Разделяем входную строку на 2 по пробелу if(count($nameArr) == 2){ //проверяем сколько слов было указано — если 2 то делаем запрос. Если 1, то нужно делать как у Вас было $getName = mysql_query(' SELECT * FROM users WHERE firstname LIKE "%'.mysql_real_escape_string(addslashes($searchq)).'%" OR lastname LIKE "%'.mysql_real_escape_string(addslashes($searchq)).'%" OR ( firstname LIKE "%'.mysql_real_escape_string(addslashes($nameArr[0])).'%" AND lastname LIKE "%'.mysql_real_escape_string(addslashes($nameArr[1])).'%" ) '); } if(!empty($searchq)) while ($row = mysql_fetch_array($getName)) echo $row['firstname']." ".$row['lastname'] . '<br/>'; ?> 

  • the code is not secure. - Visman
  • Well, we are still helping to write an algorithm and then do the checks - Mihanik71
  • @Visman why? - BedOmar
  • 2
    @ Mihanik71, this is the wrong approach, since he will receive his answer and leave, and someone else will apply this leaky solution in his own way. - Visman
  • The code is working but tell me please what holes are there? and how to fix them - BedOmar

Code or rather sql will work in the following scenario.

Suppose $ searchq = (Vasya Pupkin or Pupkin Vasya).

And the columns in the table have the following meanings

firstname = (xxxB )xxx, lastname = (xxxPupkinxxx)

or

firstname = (xxxPupkinxxx), lastname = (xxxВассяxxx)

 include('auth/bd.php'); $searchq = $_GET['name']; $searchq = addslashes($searchq); $sql = "SELECT * FROM `users` WHERE ( `firstname` LIKE CONCAT('%',SUBSTRING_INDEX('".$searchq."', ' ', 1),'%') AND `lastname` LIKE CONCAT('%',SUBSTRING_INDEX('".$searchq."', ' ', - 1),'%') ) OR ( `firstname` LIKE CONCAT('%',SUBSTRING_INDEX('".$searchq."', ' ', - 1),'%') AND `lastname` LIKE CONCAT('%',SUBSTRING_INDEX('".$searchq."', ' ', 1),'%') )"; $getName = mysql_query($sql); if(!empty($searchq)) while ($row = mysql_fetch_array($getName)) echo $row['firstname']." ".$row['lastname'] . '<br/>'; 

And by the way, without using the mysql_connect, mysql_query function ...

Use PDO

I hope helped. Good luck

  • The code is working, but I have a live search for this. I need to find a person when I enter the first few letters of the name, the person is with this code only when all the corresponding characters are entered, I’ll find Pupkin only when I enter Vasya Pupkin, and It is necessary that when I enter "Belly Button" I found all users at once with such a surname, if I correct the code for my "requirements" I choose as the best answer - BedOmar
  • @BedOmar Replace AND with OR - Vanya Avchyan
  • Mda better than caste on mysql you can’t foretell anything - Naumov

Well now I can not help but answer how to organize a search:

  1. Устанавливаем sphinx и пишем конфигурационный фаил 2. Запускаем его демон 3. Интегрируемся с api 4. Получаем результат... 

Sphinx docks can be found at the link: http://sphinxsearch.com/

More info info here https://habrahabr.ru/en/post/104690/