简体   繁体   中英

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 processed 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 '%\%%';

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM