使用PHP进行产品搜索

I'm looking for a product search function for an onlineshop. The shop has 3500 items. The items can be found in a mysql database or in a file. I want to get the products which match the search term best. The search term should be compared with the productname.

Because a search function is one of the most important parts of an onlineshop i'm looking for a very efficent solution. Does someone know a solution which is easy to implement? It's doen't need to be for free.

UPDATE:

What i tryed until now is to compare the term with the productname. First i'm looking if the term could be found 1on1. After that i take the single words in the term and check if they could be found in productnames. This works, but it's not accurate enough.

If you are atleast familiar with php and know how to query database, i suggest you to use this documentation, eventually you will know what to do with it.

http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html

In short, LIKE is something like comparasion operator, just match by pattern. In your example lets say productname LIKE "%searchterm%".

I hope this can help you. Regards

That's how you run a query and print the product info. Of course you would need to make it better looking, since there are more columns.

$res = mysqli_query($connection ,"SELECT * FROM `products` WHERE productname like '%xxx%'");

while($row = mysqli_fetch_array($res))
{
     echo $row['productname'] . '<br>';
}

You say you want a simple solution, yet you have noticed problems.

Try something like :

http://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html

I'd say this is one level of complexity up from using LIKE, but the results are miles better.

If you need better than that, then I'd say you need to rethink just searching dumb labels, and you need to start addressing how you can permit your users to filter your data.

e.g. is a "Canon 5D MK III" a lens or a camera?

These searches get expensive in terms of processing, why allow someone to search your db with the word "Canon" pulling back possibly hundreds of results without asking if they are looking for a camera or a lens?

Being able to permit that kind of filtering would suggest that your database had columns allowing you to split all the cameras from the lenses from the attachments.

So, search is good, but filtering the meta data describing each row is better, if you do not already do this then you might consider starting this process. I would suggest that the benefits you will reap will far outweigh offering a search on your website.