开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜