如果存在于数据库中,则username应该是唯一的,然后返回错

时间:2014-03-21 07:18:30

标签: php mysql

我正在处理一个项目,我想根据用户检查登录凭据。当有新用户登录时,如果用户名存在于数据库中,则会产生错误!

username exist 

代码:

include('connect.php'); 

    $query = mysql_query("SELECT * FROM users WHERE user_name = '". $user_name ."' "); 

    if (mysql_num_rows($query) > 0) 
    { 
     echo 'Username already in use please try another.'; 
    }
    else{
    $opt=$obj->addStudent($user_name,$user_pass,$user_email,$user_contact);
    }
}

我想检查忽略区分大小写。

2 个答案:

答案 0 :(得分:1)

include('connect.php'); 

    $query = mysql_query("SELECT * FROM users WHERE UPPER(user_name) = '". mysql_real_escape_string(strtoupper($user_name)) ."' "); 

    if (mysql_num_rows($query) > 0) 
    { 
     echo 'Username already in use please try another.'; 
    }
    else{
    $opt=$obj->addStudent($user_name,$user_pass,$user_email,$user_contact);
    }
}

这样用户名:XyZ将与xyz相同

答案 1 :(得分:0)

使用更快的代码进行唯一名称测试。

include('connect.php'); 

    $query = mysql_query("SELECT `user_name` FROM users WHERE LOWER(user_name) = LOWER(". $user_name .")"); 

    if (mysql_num_rows($query) > 0) 
    { 
     echo 'Username already in use please try another.'; 
    }
    else{
    $opt=$obj->addStudent($user_name,$user_pass,$user_email,$user_contact);
    }