'How to use strtolower() in a query in PHP

In a database I have this field: "TeST". I don’t know where the upper case characters are. I just want to strtolower them and do something like this:

SELECT * FROM table WHERE strtolower(field) = strtolower($var)

How can I do that?



Solution 1:[1]

In MySQL, the function is called LOWER Then again, you can just use a case-insensitive collation on the field or in the query, and it will match regardless of case, which seems the better option.

Solution 2:[2]

Simply use:

"SELECT * FROM `table_name` WHERE LOWER(`field_name`)='" . strtolower($_var) . "'";

Or use:

"SELECT * FROM `table_name` WHERE LCASE(`field_name`)='" . strtolower($_var) . "'";

Both functions works same.

Solution 3:[3]

The LOWER function in MySQL can be used to convert a field value to lower case.

For example:

"select * from table_name where LOWER(email) = ?";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wrikken
Solution 2 Peter Mortensen
Solution 3 Peter Mortensen