0

My code is working but not really how I want it to. Right now if I search for "Tom Trio" it does show all the results of rows that contain the name "Tom Trio" but if I search for "Tom Johnson" it shows that no results where found. I want that even when I search "Tom Johnson" it still display "Tom Trio Johnson" profile as part of the results it found from the database. How do I go about that?

I have a database table as follows:

|  Full Name             |  Age  |  Gender |
|------------------------|-------|---------|
|  Tom Trio Johnson      |  23   |  Male   |
|  Silvia Johnson Calvin |  27   |  Female |
|  James Doe             |  16   |  Male   |

code for index.php is as follows:

<form action="results.php" method="post">
 <input type="text" placeholder="Search..." name="search_results"/>
 <button type="submit">Submit</button>
</form>

Code for results.php:

<?php 

   $searchResults = mysqli_real_escape_string($conn, $_POST['search_results']);

   $result = mysqli_query($conn,"SELECT * FROM guests WHERE fullName LIKE '%$searchResults%'");
   while($row = mysqli_fetch_assoc($result)){
      echo $searchResults . $row['Age'] . $row['Gender']; 
   }
   mysqli_close($conn);
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

1

First, do not munge queries with literal values. Learn to use parameters.

Second, you probably really want full text search. But, you can handle your specific question by using wildcards with like:

fullName LIKE replace(?, ' ', '%')

The ? is a parameter placeholder.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786