我正在尝试使用一些记住我的课程,现在的问题是我得到了
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的新手,我做错了什么?
似乎没有构造函数。如果在使用时传递变量new
,则传递给构造函数而不是setter
。所以当使用这个:
$storage = new Rememberme_Storage_PDO($connection);
……您需要Rememberme_Storage_PDO
:
public function __construct(\PDO $connection) {
$this->setConnection($connection);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句