Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL and PHP search for similar words

Tags:

php

select

mysql

My MySQL table contains items which have the attributes name(name of item) and keywords(words related to the item). I search the database with the following code..

SELECT * FROM items WHERE (name LIKE '%$query%' OR keywords LIKE '%$query%') LIMIT $start, $per_page"

One item is a phone and it has a keyword "mobile", so when someone searches mobile the item appears as a result which is fine. But when someone searches "mobiles" with the s on the end it displays no results. How can I make my query search for anything that includes the search term?

like image 679
Gregor Avatar asked Oct 15 '25 14:10

Gregor


1 Answers

Assuming this was down-voted due to lack of detail, I am editing with an explanation.

The Like operator matches a strict pattern of characters with some wildcard characters. What you are asking the database to do is interpret natural language variants on search terms to account for things like plurals. You cannot do that with pattern matching alone. There is no good, reliable way to match on a part of a search term with the Like or similar operator (we can write code to remove any trailing spaces in the search in an attempt to match on more items, for example). I recommend you research the following, a capability common to all major RDBMS platforms such as MS SQL:

Natural Language Full-Text Searches:

http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html

That link is a reference to the feature complete with sample code.

like image 134
Lou Avatar answered Oct 17 '25 05:10

Lou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!