ensuring search lowercase characters
How do I search lower case in MySQL?
I have the f开发者_JAVA百科ollowing
$sql = "SELECT * FROM TABLE_NAME WHERE column LIKE '%$search%'";
How do I make sure the values in "column" are matched to lowercase?
You should either set your column's collation to a case-sensitive one, like UTF8_BIN
, or make an additional check in the filter:
SELECT *
FROM table_name
WHERE column LIKE '%$search$%'
AND column COLLATE UTF8_BIN = LOWER(column COLLATE UTF8_BIN)
Note that if your collation is case-insensitive, like UTF8_GENERAL_CI
, then the following query:
SELECT LOWER('A') = ('A')
will return true. You should explicitly coerce your column to a case-insensitive collation before doing a comparison.
Assuming I'm interpreting correctly, use the MySQL LOWER()
function:
$sql = "SELECT * FROM TABLE_NAME WHERE LOWER(column) LIKE '%$search%'";
This will convert everything in column
to lowercase before testing it against the search value.
精彩评论