简体   繁体   English

Php Sql 使 select 语句不区分大小写

[英]Php Sql making select statement case insensitive

So i basically trying to check if username exists or not but problem is people impersonating others by changing capitalazation I need prevent that i read some answers but they for "LIKE" operator in sql not the所以我基本上试图检查用户名是否存在,但问题是人们通过改变大写来冒充他人我需要防止我阅读一些答案,但他们是 sql 中的“LIKE”运算符而不是

SELECT * FROM users WHERE nick=? LIMIT 1

Ho i can make this return true on different capitilization too Example: test exist on database but if users try to register teST it should return it exists or tEsT我也可以在不同的大写上使它返回 true 示例:数据库上存在测试,但如果用户尝试注册 testST 它应该返回它存在或 tEsT

[Database] [数据库]

1

I tried doing this:我试过这样做:

$user_check_query = "SELECT * FROM users WHERE nick COLLATE utf8_bin = ? LIMIT 1";
$dts = $db->prepare($user_check_query);
$dts->bind_param('s', $nick);
$dts->execute();

It still does not help.它仍然没有帮助。

Try with a binary collation:尝试使用二进制排序规则:

SELECT * 
  FROM users 
  WHERE username COLLATE utf8_bin = 'NicK'

if your column is already has collation that is not case sensitive, then the query should be ok, if it is case sensitive you can change your query如果您的列已经有不区分大小写的排序规则,那么查询应该没问题,如果区分大小写,您可以更改查询

SELECT * FROM users WHERE lower(username) =lower('nick') LIMIT 1

but to stop that you need to make a unique constraint on that column:但要停止这种情况,您需要对该列进行唯一约束:

alter table users 
add constraint unique_usernames UNIQUE (username) 

considering collation is not case sensitive.考虑排序规则不区分大小写。

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

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