penpen
penpen

Reputation: 935

MySQL Apostrophe- How to escape and return correct results with MATCH/AGAINST

I have a table sort of like this:

TABLE BOOKS
ID   TITLE             SUBTITLE
------------------------------------
1    Don't Forget      
2    Twenty/Twenty      How To Subtitle
3    Red Riding Hood    Another Subtitle
4    The Three Bears
5    The Threev Bears

MySQL is:

SELECT * FROM books WHERE MATCH (titl, subt) AGAINST ('+don\'t*' IN BOOLEAN MODE)

This Query is returning for me (in phpMyAdmin, I'm still not sure how to escape the apostrophe in PHP) the following:

   ID  TITLE
   -------------------------------
   1   Twenty/Twenty
   2   The Threev Bears

I'm not sure why I'm getting "The Threev Bears" and not "The Three Bears" too? Is "Three" an overlooked word?

Also why am I getting "Twenty/Twenty" but not "Don't Forget"? That is the only result I want from this.

Thank you for your help!

Upvotes: 0

Views: 964

Answers (1)

Code Magician
Code Magician

Reputation: 23972

You've got a few questions in here so let me try to address them.

First, you are correct three is indeed an overlooked word. MySQL has, by default, a list of ignored or "stop" words which aren't indexed. You can adjust this. The idea is to avoid cluttering your index with very common words that will return a large percentage of the results.

To escape characters in PHP you should really be using parameterized queries or at the very least using msql_real_escape_string. Here's a great SO post on the subject

Finally, the reason you're getting Twenty/Twenty, but not Don't forget is because Don't is also a stopword. TwentyTwenty is matching on the t* I believe.

Upvotes: 1

Related Questions