Php 密码检索,但显示“对非对象调用成员函数prepare()”

Php 密码检索,但显示“对非对象调用成员函数prepare()”,php,Php,我很难找到解决此错误的方法: 对中的非对象调用成员函数prepare 第6行的F:\Server\xamplite\htdocs\verif.php 每当我试图编译脚本时,就会出现这个错误。 我不知道在哪里插入数据库连接。 以下是脚本: $username = 'Admin'; $password = 'password'; $sth = $dbh->prepare(' SELECT hash FROM users WHERE username = :user

我很难找到解决此错误的方法:

对中的非对象调用成员函数prepare 第6行的F:\Server\xamplite\htdocs\verif.php

每当我试图编译脚本时,就会出现这个错误。 我不知道在哪里插入数据库连接。 以下是脚本:

$username = 'Admin';
$password = 'password';

$sth = $dbh->prepare('
  SELECT
    hash
  FROM users
  WHERE
    username = :username
  LIMIT 1
  ');

$sth->bindParam(':username', $username);

$sth->execute();

$user = $sth->fetch(PDO::FETCH_OBJ);

// Hashing the password with its hash as the salt returns the same hash
if ( crypt($password, $user->hash) == $user->hash ) {
  // Ok!
}
$dbh从未在粘贴的代码中初始化

您可以这样做,假设您使用的是mysql数据库,数据库名称为testdb,并且您的数据库托管在与127.0.0.1上托管的应用程序相同的服务器上:


更多信息请参见。

$dbh->prepare做什么?它是如何定义的?即使我的问题不够清楚,你的回答也非常有效。我对PHP和PDO都是新手。非常感谢你的帮助!