Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL lowercase to compare data

Tags:

php

mysql

I want to get the contents from a row in the database and compare the lowercase version of it to a lowercase version of a user imput to check if it exists in the database:

"SELECT `id` FROM `user_accounts` WHERE `username` = '".strtolower($username)."'"

How can i get username to be lowercase from mysql?

like image 713
Dylan Cross Avatar asked Apr 02 '12 02:04

Dylan Cross


People also ask

How do I use lowercase in MySQL?

The LOWER() function converts a string to lower-case. Note: The LCASE() function is equal to the LOWER() function.

Is data case-sensitive in MySQL?

Table and database names are stored on disk using the lettercase specified in the CREATE TABLE or CREATE DATABASE statement, but MySQL converts them to lowercase on lookup. Name comparisons are not case-sensitive. This works only on file systems that are not case-sensitive!

How do I make a case-insensitive in MySQL?

If you want case-insensitive distinct, you need to use UPPER() or LOWER().

Is MySQL like case-sensitive?

By default, it depends on the operating system and its case sensitivity. This means MySQL is case-insensitive in Windows and macOS, while it is case-sensitive in most Linux systems. However, you can change the behavior by changing collation.


1 Answers

As answered above, Ideally this should work,

$query = "SELECT `id` 
          FROM `user_accounts` 
          WHERE LOWER(`username`) = '".strtolower($username)."'";

but it won't work if the "username" column in "user_accounts" table is defined as VARBINARY. The reason is VARBINARY requires the data to be case sensitive

like image 111
Jake Avatar answered Oct 20 '22 15:10

Jake