我检查重复的电子邮件时遇到小问题,我不知道我做错了什么。 PHP SIDE:
// convert to lower case
$email = $db->real_escape_string(trim(strtolower($_POST['email'])));
$q = $db->query("SELECT email FROM user WHERE email='$email'");
$r = $q->fetch_assoc();
if($email == $r['email']) {
echo 'yes';
} else {
echo 'no';
}
我在数据库中有一条旧记录。有些电子邮件混合了大写和小写!其中大部分来自hotmail电子邮件帐户!如何检查甚至包含大写和小写的电子邮件?
ie: Donald1990@hotmail.com // it skips the duplicate error?
Hedmunds@hotmail.co.uk
hedmunds@hotmail.co.uk
答案 0 :(得分:2)
其实我觉得你想要
"SELECT LOWER(email) FROM user WHERE LOWER(email)='$email'"
答案 1 :(得分:1)
我正在使用laravel 5,但概念是相同的
//检查1到500条记录,如果1到5000或更多依赖于数据量,某些数据库将超时,您可能需要增加超时限制
$users = Subscribers::all(); // take all users from DB
for ($x = $check_from; $x <= $check_to; $x++){
$check_user = Subscribers::select('id','email')->where('id',$x)->first();
if (!isset($check_user)){ // check for gaps in betw records
echo $x . "- not set<br>";
}
else{
$check_user_id = strtolower(trim($check_user->id));
$check_user_email = strtolower(trim($check_user->email));
foreach ($users as $user){
$user_id = strtolower(trim($user->id));
$user_email = strtolower(trim($user->email));
if ($check_user_email == $user_email AND $check_user_id !== $user_id ){
echo $check_user_id.' - '.$user_id.' - '. $user_email.' - Duplicated records<br>';
//$user->delete();
// choose to delete duplicate or whatever u wanna do with it
}
}
}
}
//需要检查电子邮件是否相同&amp;&amp; user_id不相同,那么将有2个id与相同的电子邮件 我的代码有效,但是如果有人可以改进以使其缩短会更好。