PHP MYSQL 查询数据库时检查大小写

发布于 2024-12-06 06:33:18 字数 216 浏览 0 评论 0原文

查询数据库时如何检查字符串的大小写?例如,使用以下语句:

mysql_query("SELECT id FROM members WHERE username = '$username'");

数据库中的值为“Test”。然而,用户可以输入“test”,它仍然可以工作。如何查看具体情况?我尝试过 === 和 ==,但显然它们在 SQL 中不起作用。

How do I check the case of a string when querying the database? For example, with this statement:

mysql_query("SELECT id FROM members WHERE username = '$username'");

The value in the database is "Test". The user could, however, type in "test" and it would still work. How do I check the exact case? I've tried === and ==, but apparently they don't work in SQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金兰素衣 2024-12-13 06:33:18

您的排序规则需要区分大小写。

将用户名列的排序规则更改为区分大小写的排序规则。
例如,如果您仅在用户名列中使用拉丁字符,请将其更改为 latin1_general_cslatin7_general_cs

ALTER TABLE `members` CHANGE `username` `username` VARCHAR( 500 ) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL 

ALTER TABLE `members` CHANGE `username` `username` VARCHAR( 500 ) CHARACTER SET latin7 COLLATE latin7_general_cs NOT NULL 

Your collation needs to be case sensitive.

Change the collation of the username column to a case sensitive one.
For example if you are using latin characters only in the username column, then change it to latin1_general_cs or latin7_general_cs

ALTER TABLE `members` CHANGE `username` `username` VARCHAR( 500 ) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL 

or

ALTER TABLE `members` CHANGE `username` `username` VARCHAR( 500 ) CHARACTER SET latin7 COLLATE latin7_general_cs NOT NULL 
鸵鸟症 2024-12-13 06:33:18

您使用什么 SQL 服务器?鉴于您使用的是 PHP,我将假设它是 mysql 并向您发送 此处

What SQL server are you using? Given that you're using PHP, I'm going to assume it's mysql and send you here.

怪我太投入 2024-12-13 06:33:18

使用 == 运算符检查数据库的值,例如:

$row=mysql_query("SELECT id FROM members WHERE username = '$username'");
while($rowData=mysql_fetch_assoc($row)) {/* fetch the value */
  if($rowData['value']=='test') {
    echo "yes value is correct";/* code for statement */
  }
}

Use the == operator to check the value of the database for example:

$row=mysql_query("SELECT id FROM members WHERE username = '$username'");
while($rowData=mysql_fetch_assoc($row)) {/* fetch the value */
  if($rowData['value']=='test') {
    echo "yes value is correct";/* code for statement */
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文