kjtocool Posted April 28, 2008 Share Posted April 28, 2008 I want the user to be able to enter a string, say: "Dog's Play" I have a database filled with various strings like: Dog at Play Monkey Town Dog in Play Doggies Play John and Sally Playground Amy T Dog World What is the best way to utilize PHP and MySQL to return partial and related strings from the database while excluding the irrelevant? Quote Link to comment https://forums.phpfreaks.com/topic/103291-string-comparison-how-to-return-similar-strings/ Share on other sites More sharing options...
wildteen88 Posted April 28, 2008 Share Posted April 28, 2008 MySQL supports Wildcards within queries, Quote Link to comment https://forums.phpfreaks.com/topic/103291-string-comparison-how-to-return-similar-strings/#findComment-529005 Share on other sites More sharing options...
Rohan Shenoy Posted April 28, 2008 Share Posted April 28, 2008 Use the LIKE statement $sql="SELECT * FROM table_name WHERE filed_name LIKE '%$string%'"; If you want to sort the results in terms of relevance to the search string, then have a look at http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html (see for the one Posted by Brad Satoris on December 13 2004 10:14pm in the middle of that page) Quote Link to comment https://forums.phpfreaks.com/topic/103291-string-comparison-how-to-return-similar-strings/#findComment-529008 Share on other sites More sharing options...
kjtocool Posted April 28, 2008 Author Share Posted April 28, 2008 Thanks, did a little testing, seems like the LIKE statement will help a lot, but it seems to only do a test to see if the items in a specific column contain the string given. For example, if the column in the database has "Prom" and you do a LIKE search for porm, you will get no results. Is there a simple way to also test for minor misspelling errors like the above? Quote Link to comment https://forums.phpfreaks.com/topic/103291-string-comparison-how-to-return-similar-strings/#findComment-529039 Share on other sites More sharing options...
dptr1988 Posted April 28, 2008 Share Posted April 28, 2008 No. the 'LIKE' operater is just about the best the MySQL can offer. If you know how to generate a list of different misspelliings you could list them all in the query like this: $sql="SELECT * FROM table_name WHERE field_name LIKE '%$string%' OR " . "field_name LIKE '%$misspelling1%' OR " . "field_name LIKE '%$misspelling2%' OR " . "field_name LIKE '%$misspelling3%' OR " . "field_name LIKE '%$misspelling4%' "; Quote Link to comment https://forums.phpfreaks.com/topic/103291-string-comparison-how-to-return-similar-strings/#findComment-529049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.