使用 PHP MYSQL 搜索查询搜索每个搜索词 [英] Search Each Word Of a Search Using PHP MYSQL Search Query
问题描述
我想根据搜索栏中输入的关键字获取记录.
假设我的 SQL 表的列中有以下 3 条记录
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
- 漂亮的小孩子.
- 动物园里的美丽兔子.
- 天然水.
现在,如果搜索查询包含 Beautiful
,它将返回前 2 条记录.
如果搜索查询包含Beautiful (anything)
,则不会返回任何内容.
Now, If the Search Query contains Beautiful
, It will Return First 2 Records.
If the Search Query contains Beautiful (anything)
, It will Return Nothing.
我也希望在这种情况下显示前 2 条记录,因为它与上面搜索的查询具有相同的词 beautiful
.
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.
现在,我正在使用SELECT * FROM table WHERE name LIKE '%value%' ORDER BY id ASC
是否有其他查询或方法可以实现此类结果?
Is there any Other Query or Method to Achieve Such Sort Of Results ?
推荐答案
SELECT * FROM table WHERE (name LIKE '%value1%') OR (name LIKE '%value2%') ORDER BY id ASC代码>
等
因此,您必须将搜索字符串拆分为单独的词.
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";
这篇关于使用 PHP MYSQL 搜索查询搜索每个搜索词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!