I currently have a simple search engine which searches a column in my database based on input from a user:
$search = $_GET['search'];
$terms = explode(" ", $search);
$sql = "SELECT * FROM people WHERE lname LIKE :search";
$q = $conn->prepare($sql) or die("failed!");
$q->bindValue(':search',"%".$search."%",PDO::PARAM_STR);
$q->execute();
if ($q){
//
do something
}
Currently it is searching for terms as a whole, for example “red desk” returns a result BUT “desk red” does not return anything
Any ideas? Any help much appreciated!
EDIT:
I have since changed it to this…
$search = $_GET['search'];
$terms = explode(" ", $search);
$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)";
$q = $conn->prepare($sql) or die("failed!");
$q->bindValue(':search',"%".$search."%",PDO::PARAM_STR);
$q->execute();
it seems to be working okay for now, if anyone could suggest a better solution i would be very thankful!
This might not be the best way but I think something like this might do the job. I would myself love to know if there’s a smarter way to do this!