php mysql query where description like any of the word in the given string?

Almost all website having a search field on the top it will search in the entire database and display related data in the user interface.so when user type a group of word almost all case user will not get accurate report,because the MySQL query using the LIKE “%$query%/”,so in this tutorial we are planning to elaborate how to search word by word in the database for given string.


#search Form

<form action="search.php" method="post">
<input type="text" name="q" value="">

</form>

#search.php
$q=$_POST['q'];

$searchQ=explode(' ',$q );
$searchquery="";
foreach($searchQ as $key=>$value)
{
if($searchquery!='')
{
$searchquery.="description LIKE %".$value."%"
}
else
{
$searchquery.=" OR description LIKE %".$value."%"
}
}
$query="SELECT * FROM leads WHERE ".$searchquery;
you can use this query to fetch the data

Leave a Reply

Your email address will not be published. Required fields are marked *