MySQL query : search keyword like %
I was trying to query a database table by search keyword. My SQL is as follow:
SELECT * FROM some_table WHERE some_name LIKE '%$keyword%'
where $keyword is from PHP form POST data. The $keyword is already proces开发者_JAVA技巧sed by real_escape_string
function. I notice that it the $keyword is %, all records are selected. How can I search for the some_name
field with % in the content?
You have to escape the literal %
, as explained in the manual:
SELECT * FROM some_table WHERE some_name LIKE '%\%%';
EDIT: Also note that you should escape the _
character the same way, as that's symbol for a single-character wild card.
You should escape %
with \%
in keyword
精彩评论