0

I am creating searching functionality in PHP. Whenever I search using my query, it returns texts that are similar to searched keyword/text. I want to output with the text/data that are closest to searched keyword.

For Example: If I search 'omkar' keyword then query will have to return all the records of 'omkar' as well as closest text such as 'oskar','onkar','omkr' etc.

I am using next MySQL query with PHP:

$que = mysql_query("select * from table where name like '%{$name}%'"

This code works and performs similar words/texts, but I want to display the closest word too.

Roman Melnyk
  • 1,097
  • 1
  • 8
  • 21
Omkar
  • 298
  • 5
  • 27
  • There are several questions in SO that address that problem too, like http://stackoverflow.com/questions/16538675/mysql-similar-text-search and http://stackoverflow.com/questions/13909885/how-to-add-levenshtein-function-in-mysql. – syck Oct 02 '15 at 13:06

1 Answers1

0

I'd use "FULL TEXT SEARCH" index for this. It is designed for exact same purposes.

see: - https://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html - https://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

Besides the performance, it gives you additional syntax for building queries such as:

mysql> SELECT * FROM articles WHERE MATCH (title,body)
    -> AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);
ioseb
  • 16,625
  • 3
  • 33
  • 29