简体   繁体   English

PHP:如果用户存在,则PDO检查不起作用

[英]PHP: PDO if user exists check doesn't work

I'm new to PHP, I created User class/object which has a function that checks for user, it returns $stmt->rowCount() but when I check for it in code it just skip it, register the user.. 我是PHP的新手,我创建了User类/对象,该类具有检查用户的功能,它返回$stmt->rowCount()但是当我在代码中检查它时,只需跳过它,即可注册用户。

Here is the code: 这是代码:

if(empty($name_err) && empty($email_err) && empty($username_err) && empty($password_err) && empty($confirm_password_err)) {   
            if($user->doesUserExist($email) === 0) {
                    $password = password_hash($password, PASSWORD_DEFAULT);
                    $user->register($username, $name, $email, $password); 
                } else {
                    $global_err = "Email is already taken";
                }
         }

Here is the function from User class: 这是User类的函数:

public function doesUserExist($email) {
            $query = "SELECT * FROM users WHERE email = :email";
            $stmt = $this->connection->prepare($query);
            $stmt->bindParam(':email', $email);
            $stmt->execute();
            $rowcount = $stmt->rowCount($stmt);
            return $rowcount;

        }

instead of === in your condition of if($user->doesUserExist($email) === 0) try to change to == and then try to change your method like this: if($user->doesUserExist($email) === 0)的情况下,而不是===尝试更改为== ,然后尝试像这样更改您的方法:

public function doesUserExist($email) {
            $query = "SELECT * FROM users WHERE email = '" . $email . "';";
            $stmt = $this->connection->prepare($query);
            $stmt->execute();
            $stmt->setFetchMode(PDO::FETCH_ASSOC);
            $rowcount = count($stmt->fetchAll());
            return $rowcount;

 }

You should Try 你应该试试

Call the function 调用函数

$checkemail=$api->checkuseremail($email);
if($checkemail)
{
    echo $mail_check;
}

and define your function like this 并像这样定义你的功能

public function checkuseremail($email)
{
    $db=getDB();
    $stmt = $db->prepare("Select id from table_name where email=:email");
    $stmt->bindParam("email", $email, PDO::PARAM_STR);
    $stmt->execute();
    $count = $stmt->rowCount();
    if($count)
    {
        return true;
    }
    else
    {
        return false;
    }
}

It will return email are already exist or not 它将返回电子邮件已经存在或不存在

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

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