这是一个典型的整理问题。默认排序规则比较不区分大小写。您应该使用二进制排序规则进行比较。
默认 utf8 排序规则
mysql> SELECT 'password' COLLATE utf8_unicode_ci = 'PASSWORD' COLLATE utf8_unicode_ci;
+-------------------------------------------------------------------------+
| 'password' COLLATE utf8_unicode_ci = 'PASSWORD' COLLATE utf8_unicode_ci |
+-------------------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)
utf8 二进制排序规则
mysql> SELECT 'password' COLLATE utf8_bin = 'PASSWORD' COLLATE utf8_bin;
+-----------------------------------------------------------+
| 'password' COLLATE utf8_bin = 'PASSWORD' COLLATE utf8_bin |
+-----------------------------------------------------------+
| 0 |
+-----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT 'password' COLLATE utf8_unicode_ci = 'password' COLLATE utf8_unicode_ci;
+-------------------------------------------------------------------------+
| 'password' COLLATE utf8_unicode_ci = 'password' COLLATE utf8_unicode_ci |
+-------------------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)
将您的查询更改为
$query = "SELECT id FROM users WHERE username = ? AND password COLLATE utf8_bin = ?";
在此处阅读有关排序规则的更多信息:
https://dev.mysql.com/doc/refman/5.5/en/charset-general.html