How do you encode an apostrophe so that it's searchable in mysql?

Posted by Yegor on Stack Overflow See other posts from Stack Overflow or by Yegor
Published on 2009-03-06T22:56:51Z Indexed on 2010/04/14 5:53 UTC
Read the original article Hit count: 207

Filed under:
|

I don't think that was the most clear question, but an example should make it a little clearer.

I have a table filled with movie names, some of which contain apostrophes. I have a search box which is used to find movies.

If I perform searches via

mov_title = '$search_keywords'

it all works, but this method will not yield any results for partial searches, so I have to use this

mov_title LIKE '%$search_keywords%'

This method works fine for titles that are A-Za-z0-9, but if a title has an apostrophe, it's not able to find the movie, even if I do an exact match.

Before the titles are stored in the DB, I put them through this:

$search_keywords = htmlspecialchars(mysql_escape_string($_GET["search_keywords"]));

So in the DB, there is a forward slash before every single apostrophe.

The only way to match a movie title with an apostrophe is to physically put a forward slash in front of the apostrophe, in the search box.

This seems so trivial, and I'm sure the solution is painfully obvious, but I'm just not seeing it.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php