duanhuang1699 2013-09-25 10:16
浏览 49
已采纳

在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 ?

  • 写回答

2条回答 默认 最新

  • douxing9567 2013-09-25 10:24
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路