Search code examples
phpmysqlsql-like

php mysql LIKE with special characters does not work properly


So i am trying to do a LIKE query and get some results but the text that i pass has some special characters that break the query.

if we assume that the text is something like this:

var test `select` `query`="$newval + "dsadsa$ ? "$test ?

and i also have exactly the same text inside a column as VARCHAR

and then executing the query

SELECT * FROM table WHERE column LIKE '%$text%'

says that there is no rows to return.

EDIT: when i post the data inside the database i simply use mysql real escape string and when i show the text where i click to search i put htmlentities on the text then i substr it from 0 to 50 and do the search query


Solution

  • You can use mysql_real_escape_string() which will escape any special characters in your string.