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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里