dongqiulei6805 2016-02-08 21:26
浏览 93

PHP致命错误:在pdo登录中的非对象上调用成员函数fetchAll()

I've been working on a web app for a client, and came across this error when building the database login code.

The config.php code

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

And the login.php code

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

The Register.php works, and adds the fields to the database, but I'll add to make sure I'm not missing something, since it is trying to access the database to verify a record.

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]);
  • 写回答

1条回答 默认 最新

  • dongwo8827523 2016-02-09 06:21
    关注

    You are using the wrong object($result) to fetch

    You need to use $stmt Do like this

    $users = $stmt->fetchAll();
    

    also, you need to pass the variable as an array before that

    $stmt->execute(array($_POST['email']));
    

    http://php.net/manual/en/pdostatement.fetchall.php

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端