I'm using PHP with MySQL to make a basic search engine on my website to look up books that people post, but the problem is that I'm using something for example:
`title` LIKE '%".(string)$searchTerm."%'
NOTICE: The variable $searchTerm is being escaped before-hand.
I know this is widely used and works pretty good, but the problem is that, if I have a book title for example:
Writer's Reference
The result(s) would show, if someone searches for "Writer's Reference" (with no double quotes) But, if someone looks up just "Writers Reference" the result won't show up, which is why I'm here asking what may be a good idea to resolve this.
Using AJAX to query every time on every onkeyup event? Collecting every output into an array? But that might slow-down the process. Is there a way in MySQL that filters and ignores apostrophes, but at the same time, if someone uses an apostrophe it would work? A regex operation?
Thanks.
EDIT: I'm not trying to prevent SQL Injections, I'm looking for a way that if someone looks up a word without an apostrophe, the result with an apostrophe shows up, but at the moment with the LIKE %% won't show up, because it takes direct words from the database. So if the search term does not have an apostrophe and the result has one, it won't show up.
EDIT 2: As I followed the comments, I recently updated the two columns in the database to use FULLTEXT via ALTER TABLE
dbname.
bookADD FULLTEXT (title,description)
But after so, I'm using a switch case to see if the user is selecting to search in the title or description, or both, both work, as I'm using MATCH(title,description) AGAINST('".(string)$sTerm."')
but the others that look in the title or description only won't work as planned, of course, I'm having something like: MATCH(title) AGAINST('".(string)$sTerm."')
but won't work unless I make it MATCH(title,title) AGAINST('".(string)$sTerm."')
(Adding another title in the MATCH)
Also, still, when someone searches for "Writes Reference" without the apostrophe, it still won't work for some reason, might it be my php code?