PDO最后插入id返回null或没有找到方法


PDO last insert id returns null or no method found

我一直在寻找很长一段时间的一些解决方案,但他们都没有工作,尝试了所有的主题从stackoverflow和什么都没有。

这是我的连接脚本:
private function __construct() {
    try {
        $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password'));
        $this->_pdo->exec("SET NAMES utf8");
    } catch(PDOException $e) {
        die($e->getMessage());
    }
}

这是我的查询方法:

public function query($sql, $params = array()) {
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }
        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }
    return $this;
}

我不得不重新做我的帐户激活系统,并把激活码单独的表,所以当新用户来注册我需要抓取插入id放置在user_id下的激活码表。问题是我不知道如何改变我提供的脚本能够抓取那个id。

I tried $this->_pdo->lastInsertId();

$this->_db->lastInsertId();

在第一种情况下,我得到null所有的时间在第二种情况下,我没有方法lastInsertId()被发现在类

也许我把它放在错误的地方或做错了,帮助会很好,提前谢谢你;)

if($this->_query->execute()) {
    $id = $this->_pdo->lastInsertId();
    $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
    $this->_count = $this->_query->rowCount();
}