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

时间:2018-06-30 03:50:45

标签: php authentication pdo

我是PHP的新手,我创建了User类/对象,它具有检查用户的功能,它返回$stmt->rowCount(),但是当我在代码中对其进行检查时,只需跳过它,即可注册用户。

代码如下:

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";
                }
         }

这是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;

        }

2 个答案:

答案 0 :(得分:0)

===的情况代替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;

 }

答案 1 :(得分:0)

您应该尝试

调用函数

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

并这样定义您的功能

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;
    }
}

它将返回电子邮件已经存在