简体   繁体   English

根据搜索查询从数据库中返回行

[英]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 ):示例记录如下所示( itemnameitemid ):

  • blackberry curve 3g (black), 1 blackberry曲线3g(黑色),1
  • samsung ace, 3三星王牌,3
  • blackberry curve 3g (white), 2 blackberry曲线3g(白色),2
  • apple iphone 4 32gb (black), 4苹果 iphone 4 32gb(黑色),4

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:我正在寻找以下问题的答案:

  1. How can search text be matched with the items?搜索文本如何与项目匹配? For example: user may enter 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列的子字符串吗?
  2. If the user enters two words like: 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?在这种情况下如何搜索?
  3. If the user enters a very generic search text like: mobiles .如果用户输入非常通用的搜索文本,例如: mobiles In this case, all the mobile phones should be displayed.在这种情况下,应该显示所有的手机。 Matching the itemname as a sub-string will not work in this case also.在这种情况下,将itemname匹配为子字符串也不起作用。
  4. User may enter search text: apple 32gb black color .用户可以输入搜索文本: apple 32gb black color In this case, apple products of 32GB capacity and black color should be displayed.在这种情况下,应该显示 32GB 容量和黑色的苹果产品。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM