1

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

但是,现在我正在尝试获取最后插入的 id 的值,但这对我来说并不适用

如果我在类中的 processQuery 方法使用任何查询(如插入/更新/删除)执行准备语句

我已经添加了这一行 $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;

            }

    }

}

?>
4

1 回答 1

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();
于 2013-04-01T23:44:54.750 回答