如何在类的方法中从PDO返回Last Insert ID

时间:2013-04-01 23:41:23

标签: php mysql pdo

我创建了一个处理mySql查询的小类。

但是,现在我正在尝试获取最后插入的id的值,但它并不适合我

如果我在类中的processQuery方法,我使用任何查询(如insert / update / remove

)执行prepare语句

我添加了这一行$ this-> lastInsertId = $ this-> pdo-> lastInsertId;它应该给我最后插入的Id并将其存储在名为$ lastInsertId的公共变量中,然后我可以从我的代码外部访问它。

如何获取最后插入的ID以使用此类?

由于

这是我的班级

<?php

class connection {

    private $connString;
    private $userName;
    private $passCode;
    private $server;
    private $pdo;
    private $errorMessage;
    public $lastInsertId;

    private $pdo_opt = array (
                            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
                            );


    function __construct($dbName, $serverName = 'localhost'){

        //sets credentials
        $this->setConnectionCredentials($dbName, $serverName);

        //start the connect
        $this->startConnection();

    }

    function startConnection(){


            $this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt);

            if( ! $this->pdo){

                $this->errorMessage  = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
                $this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.';


            }

    }


    //this will close the PDO connection
    public function endConnection(){

        $this->pdo = null;
    }

    //return a dataset with the results
    public function getDataSet($query, $data = NULL)
    {
        $cmd = $this->pdo->prepare( $query );

        $cmd->execute($data);

        return $cmd->fetchAll();
    }


    //return a dataset with the results
    public function processQuery($query, $data = NULL)
    {
        $cmd = $this->pdo->prepare( $query );
        $this->lastInsertId = $this->pdo->lastInsertId;
        return $cmd->execute($data);
    }


    //this where you need to set new server credentials with a new case statment
    function setConnectionCredentials($dbName, $serv){

        switch($serv){

            case 'BLAH':
                $this->connString   = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
                $this->userName     = 'BLAH';
                $this->passCode     = 'BLAH';
            break;

            default:
                $this->connString   = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
                $this->userName     = 'BLAH2';
                $this->passCode     = 'BLAH2';
            break;

            }

    }

}

?>

1 个答案:

答案 0 :(得分:2)

您可以添加如下方法:

public function lastInsertId($name = NULL) {
    if(!$this->pdo) {
        throw new Exception('not connected');
    }

    return $this->pdo->lastInsertId($name);
}

它只是PDO::lastInsertId()的包装。您不需要最后一个插入ID的本地副本。如果PDO未连接,则会抛出异常。如果符合您的设计,您可以将其更改为return FALSE;

像这样使用:

$con = new connection('testdb'); 
$con->processQuery('INSERT INTO `foo` ....');
$lastInsertId = $con->lastInsertId();