[英]Returning rows from database based on search query
I am having a product table with following schema:我有一个具有以下架构的产品表:
products (itemname VARCHAR(50), itemid INT(10));
Sample records are shown below ( itemname
, itemid
):示例记录如下所示(
itemname
, itemid
):
Now suppose user enters the query into the search box of the website and accordingly results are displayed.现在假设用户在网站的搜索框中输入查询并显示相应的结果。 ie if user enters
blackberry
then Blackberry mobiles should be displayed即,如果用户输入
blackberry
则应显示 Blackberry 手机
I am looking answers for the following questions:我正在寻找以下问题的答案:
blackberry
in the search box then how it can be searched?blackberry
那么如何搜索呢? Should I search for blackberry
word as a sub-string of itemname
column of the above table?blackberry
字作为上表itemname
列的子字符串吗?apple 32gb
.apple 32gb
。 Now if I use the above mentioned method of sub-string then it wont work because there is no row in the table having apple 32gb
as sub-string.apple 32gb
作为子字符串的行。 How to search in this case?mobiles
.mobiles
。 In this case, all the mobile phones should be displayed.itemname
as a sub-string will not work in this case also.itemname
匹配为子字符串也不起作用。apple 32gb black color
.apple 32gb black color
。 In this case, apple products of 32GB capacity and black color should be displayed. I know that implementing the search 100% correct is not possible.我知道实现 100% 正确的搜索是不可能的。 I am just looking for hints/how to proceed.
我只是在寻找提示/如何进行。 I am using PHP, MySQL, Apache.
我正在使用 PHP、MySQL、Apache。
What kind of database is this, if MySQL you can enabled fulltext search on the field and then just a simple query will get you the results you expect.这是什么类型的数据库,如果 MySQL 您可以在字段上启用全文搜索,然后只需一个简单的查询即可获得您期望的结果。
for many word searching I would do something like this:对于许多单词搜索,我会做这样的事情:
$searchArray = explode(" ", $searchString);
foreach ($searchArray as $word)
{
//add an OR clause to your sql query like the following
$query .= "OR itemname like %".$word."%";
}
the other questions are far more complicated, but I hope this helps其他问题要复杂得多,但我希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.