Check if username exists PDO

后端 未结 3 439
醉话见心
醉话见心 2020-12-18 10:45

How would I be able to check multiple factors combined instead of checking for each one? So basically I\'m using PDO and I have to make sure that the usernames and emails ar

3条回答
  •  攒了一身酷
    2020-12-18 10:51

    Something like this should work:

    function userExists($db, $user)
    {
        $userQuery = "SELECT * FROM userinfo u WHERE u.user=:user;";
        $stmt = $db->prepare($userQuery);
        $stmt->execute(array(':user' => $user));
        return !!$stmt->fetch(PDO::FETCH_ASSOC);
    }
    
    $user = 'userName';
    $exists = userExists($db, $user);
    if(exists)
    {
         // user exists already.
    }
    else
    {
         // user doesn't exist already, you can savely insert him.
    }
    

提交回复
热议问题