I have a table in database which has a column named title. And in that table imagine I have a car name saved for example Toyota fj cruiser. Now If I search using % like % and search like % Toyota % it would return properly the result Toyota fj cruiser. Now if I search like % Toyota f % it would again return properly.
The issue arises when I try to search like % Toyota cru % then it does not return the title that is Toyota fj cruiser.
$sql_sticky = mysql_query("SELECT `title`, `category`, `subcategory`, `type`, `times` FROM `product_search1` WHERE `title` LIKE '%$key%' order by times desc limit 30");
Why this would happen ?
You are trying to search by breaking the word sequence.
To achieve what you need, one solution will be to split the words and add %
pattern to it.
So your keyword should become %Toyota%cru%
You can first replace the words i.e. spaces
with %
in PHP
as follows:
$str = 'Toyota cru';
$queryStr = str_replace(' ', '%', $str);
$sql = "SELECT car_name FROM tbl_name WHERE carname LIKE '%$queryStr%'";