在PDO上的非对象上调用成员函数prepare()
I am trying to use some remember me class , now the problem is i get
Call to a member function prepare() on a non-object
and i dont get any error's :
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);
i send the connection to this file : pdo.php and using there this code :
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);
}
}
}
And i have the error here on this function :
** this function inside Rememberme_Storage_PDO class
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');
}
}
says :
Fatal error: Call to a member function prepare() on a non-object in F:\wamp\wwwememberme-master\src\Rememberme\Storage\PDO.php on line 44
I am newbie at PDO , what am i doing wrong ?
我正在尝试使用一些记住我的课程,现在问题是我得到了 p>
我不会收到任何错误: p>
index.php: p>
我将连接发送到此文件:
pdo.php strong>
并使用此代码: p >
我在这个函数上有错误 : p>
** 此函数位于Rememberme_Storage_PDO类 strong> p>
说: p>
我是PDO的新手,是什么 我做错了吗? p>
div>在非对象上调用成员函数prepare() code> p>
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) );
code> pre>
类Rememberme_Storage_PDO扩展了Rememberme_Storage_DB {
/ **
*
* @var PDO
* /
protected protected connection;
public function getConnection(){
返回$ this->连接;
}
公共函数setConnection(PDO $ connection){
尝试{
$ this-> connection = $ connection;
}
catch (PDOExcep $ e)
{
printf($ e);
}
}
}
code> pre>
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(“Ymd H:i:s”,$ expire)) ))
{
die('excute faild');
}
}
code> pre>
致命错误:在第44行的F:\ wamp \ www
ememberme-master \ src \ Rememberme \ Storage \ PDO.php中的非对象上调用成员函数prepare() code> pre>
It looks like don't have a constructor. If you pass a variable when using new
it is passed to the constructor and not a setter
. So when using this:
$storage = new Rememberme_Storage_PDO($connection);
… you need in Rememberme_Storage_PDO
:
public function __construct(\PDO $connection) {
$this->setConnection($connection);
}
Clearly $this->connection is not an object .. which is rather obvious since you are not using the setConnection public function..
I only see you trying to pass the connection object to the constructor, but there is no constructor that actually stores it in the $this->connection
property.
Perhaps
$storage = new Rememberme_Storage_PDO($connection);
should be:
$storage = new Rememberme_Storage_PDO();
$storage->setConnection($connection);