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条)

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置