I have a query that contains some characters that cause a syntax error because contains reserved characters and I am battling to understand how to escape the string correctly.
The query is:
SELECT * FROM `products`
WHERE MATCH (code, description)
AGAINST (UPPER(+("intel"*) +("cpu"*)) IN BOOLEAN MODE)
But when I run this query I get the following error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') +("cpu"*)) ) IN BOOLEAN MODE)' at line 1
Okay so fine it does not like the )
as it would indicate that the AGAINST
is being closed however it is not (yet). So I tried to escape it with a backslash but it still throws the same error.
If I try this in PHP using a prepared statement while binding the search string +("intel"*) +("cpu"*)
into the statement it works. So it seems that the way that it escapes it is not with a backslash or that there is something else.
So I was looking at the PHP documentation for mysqlescapestring and I saw that it: "prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.".
Which indicate that the single and double quotes need to be escaped and I tried to do this but it just throws the same syntax error but on the double quote character, i.e. to use near '\"intel\"*\)...
I do understand that it would be best to use prepared statements and that this solves the problem but I just want to understand what I have done wrong here and how I could escape a string like this within an AGAINST
clause as I have done here.
If anyone could suggest where I have going wrong with this then it would be greatly appreciated. Thank you.