0

I try to create search system using php and sql current condition is working fine but if I want to search for John Doe (firstname + lastname) nothing happens. I try the + between firstname and lastname but it did not work.

Condition is here:

 if(isset($_POST["query"])){
   $search = mysqli_real_escape_string($conn, $_POST["query"]);
   $query = "
   SELECT * FROM users
   WHERE firstname LIKE '%".$search."%'
   OR lastname LIKE '%".$search."%'
 ";
}
GPA
  • 111
  • 2
  • 10
  • Please read https://stackoverflow.com/questions/28385145/correct-way-to-use-like-var-with-prepared-statements-mysqli and https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – Dharman Dec 23 '20 at 19:54

1 Answers1

2

You msy try something like this (using CONCAT):

$query = "SELECT * FROM users
WHERE firstname LIKE '%".$search."%'
OR lastname LIKE '%".$search."%'
OR CONCAT(firstname,' ', lastname) LIKE '%".$search."%'";
The Alpha
  • 143,660
  • 29
  • 287
  • 307