如何在PHP/MySQL中使SELECT不区分大小写? [英] How to make a SELECT in PHP/MySQL case insensitive?
问题描述
她是我的问题,我想这真的很基本.
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
:
WHERE foo LIKE 'bar'
或者您可以将以下两个字母都转换为小写字母:
Or you can cast both lowercase with:
WHERE LOWER(foo) = LOWER("bar")
使用LOWER()
的示例最有效,因为您知道数据库中的所有数据均已小写,然后可以执行:
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")
如果可以小写数据库中的所有数据,则此价格比LIKE
便宜.
This would be a cheaper comparison than the LIKE
if you can lower case all of the data in your database.
这篇关于如何在PHP/MySQL中使SELECT不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!