Search code examples
phpmysqlsqlpdolowercase

Ensuring emails are lower case when searching/inserting/updating MySQL database with PHP PDO


I am using PHP's PDO to interact with a MySQL database. I am currently using the following code to ensure an email address is always lower case to make sure there are no duplicates when inserting/updating or to find an email when searching.

Can someone tell me if this is okay the way it is or is it better to use the SQL or PHP way of converting to lower case?

$sql = "UPDATE table SET name=:name, LOWER(email)=:email WHERE id = :id";
$st->bindValue(":email", strtolower($this->email), PDO::PARAM_STR);

Solution

  • You can't write to LOWER(email) and it's also unnecessary since you are always inserting the lower-cased version via PHP anyway. Just change it to email.