-1
我一直在爲客戶端開發Web應用程序,並在構建數據庫登錄代碼時遇到此錯誤。PHP致命錯誤:調用pdo登錄中的非對象的成員函數fetchAll()
的config.php文件代碼
php try {
// Create connection
$dbh = new PDO("mysql:host=" . $localhost . ";dbname=" . $database, $username, $dbpassword);
} catch (PDOException $e) {
// Echo error message if the connection fails
echo 'Connection failed: ' . $e->getMessage();
exit;
}
而且login.php中碼
require 'config.php';
$sql = "SELECT * FROM `Users` WHERE `email` = ?";
$stmt = $dbh->prepare($sql);
$result = $stmt->execute([$_POST['email']]);
$users = $result->fetchAll();
if (isset($users[0])) {
if (password_verify($_POST['password'], $users[0]->password)) {
// valid login
include 'dashboard.php';
}
else {
// invalid password
echo "<p class=''>The password you entered is Incorrect. Please try again.</p>";
}
}
else {
// invalid email
echo "<p class=''>We're sorry, That email is not in our Records. Please Check the spelling and try again.</p>";
}
的Register.php工作,並將該領域的數據庫,但我會添加使當然,我不會錯過任何東西,因爲它試圖訪問數據庫來驗證記錄。
require 'config.php';
require 'lib/password.php';
$company = $_POST["company"];
$email = $_POST["email"];
$password = $_POST["password"];
$hash = password_hash($password, PASSWORD_BCRYPT);
$stmt = $dbh->prepare("insert into Users(id,company,email,password) VALUES (?,?,?,?)");
$stmt->execute([$id, $company, $email, $hash]);
不知道但是你在兩次執行中都使用[]嗎? – devpro
它需要作爲數組傳遞,而不是字符串。 @devpro是速記數組賦值。 – chris85
@ chris85好點。所以習慣在'array($ _ POST ['email'])'格式中看到它。 –