Search Each Word Of a Search Using PHP MYSQL Search Query

放肆的年华 提交于 2020-01-17 07:00:10

问题



I want to fetching Records On the Basis Of Entered Keywords in the Search Bar.
Suppose I have Below 3 Records in My SQL Table's Column

  1. Beautiful Small Kid.
  2. Beautiful Rabbit in the Zoo.
  3. Natural Water.

Now, If the Search Query contains Beautiful, It will Return First 2 Records.
If the Search Query contains Beautiful (anything), It will Return Nothing.

I want those First 2 Records to be Displayed in this case too, Because It has the same word beautiful like in above searched Query.

Right Now, I am Using
SELECT * FROM table WHERE name LIKE '%value%' ORDER BY id ASC

Is there any Other Query or Method to Achieve Such Sort Of Results ?


回答1:


SELECT * FROM table WHERE (name LIKE '%value1%') OR (name LIKE '%value2%') ORDER BY id ASC

etc

So, you would have to split up your search string into separate words.

$str = yourinput;
$strarray = (explode(" ",$str));
$query = "SELECT * FROM table WHERE ";
Foreach($strarray as $key=>$value){
If($key > 0){
$query = $query . "OR";
}
$query = $query . " (name LIKE '%" . $value . "%') ";
}
$query = $query . "ORDER BY id ASC";


来源:https://stackoverflow.com/questions/42859803/search-each-word-of-a-search-using-php-mysql-search-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!