Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a SELECT in PHP/MySQL case insensitive?

Tags:

php

mysql

Her's my probleme, i guess its really basic.

I'm trying to lookup in the database if a line does exist. heres my code :

$req="SELECT * FROM INSTITUTS WHERE inst_name='$fc_inst'";
$result=mysql_query($req) or die ('Erreur :'.mysql_error());
if (mysql_num_rows($result)){
echo '  name exist';
}
else {
echo '  does not exist.';
}

Probleme is, when imm looking for "test", it says does not exist, even if i have "Test" in my database.

like image 889
Clément Avatar asked May 09 '11 14:05

Clément


1 Answers

you can use LIKE:

WHERE foo LIKE 'bar'

Or you can cast both lowercase with:

WHERE LOWER(foo) = LOWER("bar")

The example with LOWER() is most effective where you know that all of your data in the database is already lower cased and then you can just execute:

WHERE foo = LOWER("bar")

This would be a cheaper comparison than the LIKE if you can lower case all of the data in your database.

like image 123
James C Avatar answered Oct 11 '22 01:10

James C