简体   繁体   English

MySQL用户名不区分大小写

[英]MySQL Username not case sensitive

sir i have problem on php mysql login 先生,我在php mysql登录时遇到问题

On my db: 在我的数据库上:

username = admin 
password = addbcea06efdd20f934b35e3b2111e55

1st i test This the query on mysql 1我测试这对MySQL的查询

SELECT COUNT(`user_id`) FROM `users` WHERE `username` = 'admin' AND `password` = 'addbcea06efdd20f934b35e3b2111e55'

the 1st result is 1 第一个结果是1

but when i my second test 但是当我第二次考试时

SELECT COUNT(`user_id`) FROM `users` WHERE `username` = 'Admin' AND `password` = 'addbcea06efdd20f934b35e3b2111e55'

why the result keep 1? 为什么结果保持1? This should be 0. I only change the username to capital letter. 应该为0。我仅将用户名更改为大写字母。 What should i do? 我该怎么办?

MySQL columns can be made IN-case-sensitive by creating them with the binary keyword. 通过使用binary关键字创建MySQL列,可以使其区分大小写。 I suspect this is your problem. 我怀疑这是您的问题。 You can modify the column to be binary or change your query to 您可以将列修改为二进制或将查询更改为

SELECT COUNT(`user_id`) FROM `users` WHERE BINARY `username` = 'Admin' AND `password` = 'addbcea06efdd20f934b35e3b2111e55'

MySQL compares data in a case-insensitive manner except for database, table and column names. MySQL以不区分大小写的方式比较数据,数据库,表和列的名称除外。 That is why your second query didn't return 0. 这就是为什么第二个查询未返回0的原因。

The best way to avoid case-sensitive problems is always to use the php function strtolower() in the two parts of data manipulation: insert and query, in this case in the registration and the login verification. 避免大小写敏感的问题的最佳方法是始终在数据操作的两个部分中使用php函数strtolower():插入和查询,在这种情况下为注册和登录验证。

You can add this line before the registration insert in your code: 您可以在注册插入代码之前添加以下行:

$username = strtolower ( $username );

This will make always the username is stored in lowercase, then you can do the same in the login part of your program. 这将使用户名始终以小写形式存储,然后您可以在程序的登录部分进行相同的操作。 Since you always will be comparing lowercases strings there is no chance of a case-sensitive error. 由于您将始终比较小写字符串,因此不会出现区分大小写的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM