我有一个带有搜索栏的网站,该网站出售手机和平板电脑。如果搜索Blackberry 9900
,则不会返回任何结果,因为该产品称为Blackberry Bold 9900
。如果缺少一个单词,我需要一种显示相关产品的解决方案。
我尝试了这个:
$words=explode("_",$name);
foreach($words as $word){
$whereClause.= ' product_name LIKE "%'.$word.'%" OR';
}
$whereClause=substr($whereClause,0,-2);
$productQuery='SELECT * FROM products WHERE'.$whereClause.' AND status=1 AND deleted=0 ORDER BY product_order';
如果搜索
Blackberry Bold 9900
,则返回所有包含Blackberry
的结果。如何解决这个问题,使结果有意义? 最佳答案
更改
$whereClause.= ' product_name LIKE "%'.$word.'%" OR';
至
$whereClause.= ' product_name LIKE "%'.$word.'%" AND';
和
$whereClause=substr($whereClause,0,-3);
当然。
这将仅搜索名称包含搜索框中所有单词的产品(但不是必须仅包含该单词)。
也许MySQL FULLTEXT搜索是更好的方法,但是它仅在MySQL 5.6之前的MyISAM表中受支持。