简体   繁体   English

检查值是否存在大写和小写字母

[英]Check value if exist with uppercase and lowercase letters

I want to check if username exists in Database but I have problem Ex : If I have username = jack and another register with username = JaCk 我想检查数据库中是否存在用户名,但是我有问题,例如:如果我有用户名= jack和另一个使用用户名= JaCk的寄存器

It accepts I want to check the full username with lowercase and uppercase and refuse such cases (jack - Jack - JaCk ....) 它接受我想用小写和大写字母检查完整的用户名,并拒绝这种情况(jack-Jack-JaCk ....)

$stmtr = $DB_con->prepare("SELECT username FROM users WHERE username=:username");
$stmtr->bindparam(":username", $val['username']);
$stmtr->execute();
$row=$stmtr->fetch(PDO::FETCH_ASSOC);

if($rowr['username']==$val['username']) {
echo "Exists";
}

There is a typo. 有一个错字。 In line: 排队:

if($rowr['username']==$val['username']) {

it should be: 它应该是:

if($row['username']==$val['username']) {

Also, by default, MySQL performs case-insensitive query. 另外,默认情况下,MySQL执行不区分大小写的查询。 Note the _ci part in your DB collation (eg utf8_general_ci ), it means case insensitive. 注意数据库排序_ci中的_ci部分(例如utf8_general_ci ),这意味着不区分大小写。 You have to perform extra checking in PHP to check for different upper- and lowercase. 您必须在PHP中执行额外的检查,以检查大小写是否不同。

 if( strtolower($row['username'])==strtolower($val['username']) ) { echo "Exists";}

You can also replace strtolower with strtoupper that will convert a string to upper case. 您也可以将strtolower替换为strtoupper ,以将字符串转换为大写。

NOTE: That for this case strtolower and strtoupper will result in the same outcome as noted by @user3783243 注意:在这种情况下, strtolowerstrtoupper将产生与@ user3783243相同的结果

Also if your trying to compare both values with Case Sensitivity that is you don't want if('JAck'== 'jAck') to return true then use strict comparison === 另外,如果您尝试将两个值与Case Sensitivity进行比较,而又不希望if('JAck'== 'jAck')返回true,则使用严格比较===

 if('JAck'== 'jAck') //will return true
 if('JAck'=== 'jAck') //will retrun false

You can using UPPER_CASE in SQL query: 您可以在SQL查询中使用UPPER_CASE

$stmtr = $DB_con->prepare("SELECT username FROM users WHERE UPPER(username)=:username");
$stmtr->bindparam(":username", strtoupper($val['username']));

But I think you should store username as uppercase or lowercase when user register. 但是我认为您应该在用户注册时将username存储为大写或小写。 It is better than using UPPER in SQL 比在SQL中使用UPPER更好

暂无
暂无

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

相关问题 反转字符串中所有字母的大小写(大写到小写,小写到大写) - Invert case of all letters in a string (uppercase to lowercase and lowercase to uppercase) 检索具有大写和小写字母的复选框值 - retrieve checkbox values with uppercase and lowercase letters .htaccess-如何将url中的大写字母和重音字母重定向到不重音小写字母? - .htaccess - How to redirect uppercase and accented letters to unaccented lowercase in url? CodeIgniter强密码策略,带有强制性的大写/小写字母和数字 - CodeIgniter strong password policy with compulsory uppercase/lowercase letters and digits 如何将所有大写字母替换为破折号,并将小写替换为正则表达式? - How do I replace all uppercase letters with a dash and lowercase with regex? PDO不在乎大写/小写字母 - PDO doesn't care about uppercase/lowercase letters MySQL搜索问题,以同时查找小写和大写字母 - MySQL search problem to find lowercase and uppercase letters at the sametime 正则表达式检查重复的字符或数字,检查小写,大写,大写 - Regular expression to check repeating character or digit, check lowercase,uppercase,capital 散列函数,不仅返回小写字母和数字,还返回符号和大写字母 - Hash function which returns not just lowercase letters and numbers, but also symbols and uppercase letters 如何在 PHP 中检查字符串是否包含大小写的特定单词 - How to Check If a String Contains a Specific Word with uppercase and lowercase in PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM