17

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel
  • 2,441
  • 4
  • 18
  • 12

4 Answers4

48

Using PDO and assuming MySQL

$stmt = $db->prepare('SELECT * FROM table WHERE LOWER(`field`) = ?');
$stmt->execute(array(strtolower($var)));
Phil
  • 157,677
  • 23
  • 242
  • 245
10

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.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
6

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vi8L
  • 958
  • 10
  • 12
4

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) = ?";
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36