簡體   English   中英

為什么此訪存函數不返回任何內容?

[英]Why isn't this fetch function returning anything?

我正在嘗試使用PDO創建登錄系統,但是由於某些原因,我的fetch功能未返回任何內容。 我試過使用print_r打印出提取信息,但它不起作用。

$sql = "SELECT * FROM users WHERE user_email=?";
$stmt = $db->prepare($sql);
$stmt->execute(array($email));
$result = $stmt->fetchAll();
$rowCount = $stmt->rowCount();

if($rowCount < 1) {
    echo "error";
    // header("Location: ../login.php?login=email_error");
    exit();
} else {
    if($row = $stmt->fetch(PDO::FETCH_ASSOC)) {}
        $hashedPwdCheck = password_verify($pwd, $row['user_pwd']);
        if($hashedPwdCheck == false) {
            exit();
        } elseif($hashedPwdCheck == true) {
            // Log in the user
            $_SESSION['id'] = $row['user_id'];
            $_SESSION['email'] = $row['user_email'];
            echo "no, here!";
            exit();
        }
    }
}

如前所述-您兩次提取數據,可以注釋掉這一行...

$result = $stmt->fetchAll();

稍后,您將獲取用戶的特定行(對fetch()的調用)

或更改以下部分...

} else {
        $hashedPwdCheck = password_verify($pwd, $result[0]['user_pwd']);
        if($hashedPwdCheck == false) {
            exit();
        } elseif($hashedPwdCheck == true) {
            // Log in the user
            $_SESSION['id'] = $result[0]['user_id'];
            $_SESSION['email'] = $result[0]['user_email'];
            echo "no, here!";
            exit();
        }
    }
}

因此,您引用回fetchAll()獲取的數據。 fetchAll()返回記錄數組時,您需要使用$result[0]引用結果集中的第一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM