开发者

MySQL query : search keyword like %

开发者 https://www.devze.com 2023-03-18 10:53 出处:网络
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%\'

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

精彩评论

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