简体   繁体   English

检查数据库中是否存在用户名的方法

[英]Method to check if username exists in database

I have this method which checks if a username exists in the database: 我有这种方法可以检查数据库中是否存在用户名:

public function checkUsername($username) 
{

    global $wpdb;
    $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users WHERE con_username =".$username);
    if(!empty($result)){

        return false;
    }

}

I am then checking the username like so: 然后,我正在像这样检查用户名:

if(!$users->checkUsername($_POST['email'])){
        $error++;
        $error_msg[]='Email already exists!';
    }

However this isnt working as it is telling me that every username i try already exists. 但是,这不起作用,因为它告诉我我尝试的每个用户名已经存在。 I dont think i have the returns written correctly.. Any ideas?? 我认为我没有正确填写退货单。

You are not returning anything if the email not found and so checkUsername will return null in this case. 如果找不到电子邮件,您将不会返回任何内容,因此在这种情况下, checkUsername将返回null

$users->checkUsername($_POST['email']) will always empty or null . $users->checkUsername($_POST['email'])将始终为emptynull

Try with - 尝试-

return empty($result);

The function should be - 该功能应为-

public function checkUsername($username) 
{

    global $wpdb;
    $result = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}users WHERE con_username ='".$username."'");
    return empty($result);

}

And check - 然后检查-

if(!$users->checkUsername($_POST['email'])){

And try to implement some security to input data. 并尝试实现一些安全性以输入数据。

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

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