我正在尝试使用一些记住我的课程,现在问题是我得到了
Call to a member function prepare() on a non-object
我不会得到任何错误:
index.php:
try {
$connection = new PDO('mysql:host=localhost;dbname=ibids', 'root', '');
}
catch (PDOException $e)
{
printf ($e);
}
$storage = new Rememberme_Storage_PDO($connection);
$rememberMe = new Rememberme($storage);
我发送连接到这个文件: 的 pdo.php 并使用此代码:
class Rememberme_Storage_PDO extends Rememberme_Storage_DB {
/**
*
* @var PDO
*/
protected $connection;
public function getConnection() {
return $this->connection;
}
public function setConnection(PDO $connection) {
try {
$this->connection = $connection;
}
catch (PDOException $e)
{
printf ($e);
}
}
}
我在这个函数上有错误:
** 此函数位于Rememberme_Storage_PDO类
中 public function storeTriplet($credential, $token, $persistentToken, $expire=0) {
$sql = "INSERT INTO {$this->tableName}({$this->credentialColumn}, " .
"{$this->tokenColumn}, {$this->persistentTokenColumn}, " .
"{$this->expiresColumn}) VALUES(?, SHA1(?), SHA1(?), ?)";
$query = $this->connection->prepare($sql);
if(!$query->execute(array($credential, $token, $persistentToken, date("Y-m-d H:i:s", $expire))))
{
die('excute faild');
}
}
说:
Fatal error: Call to a member function prepare() on a non-object in F:\wamp\www\rememberme-master\src\Rememberme\Storage\PDO.php on line 44
我是PDO的新手,我做错了什么?
答案 0 :(得分:2)
看起来好像没有构造函数。如果在使用new
时传递变量,则会将其传递给构造函数而不是setter
。所以当使用它时:
$storage = new Rememberme_Storage_PDO($connection);
...您需要Rememberme_Storage_PDO
:
public function __construct(\PDO $connection) {
$this->setConnection($connection);
}
答案 1 :(得分:0)
显然$ this->连接不是一个对象..这是显而易见的,因为你没有使用setConnection公共函数..
我只看到你试图将连接对象传递给构造函数,但是没有构造函数将它实际存储在$this->connection
属性中。
也许
$storage = new Rememberme_Storage_PDO($connection);
应该是:
$storage = new Rememberme_Storage_PDO();
$storage->setConnection($connection);