【问题标题】:How can I return LastInsertID from PDO whin a method of a class如何在类的方法中从 PDO 返回 LastInsertID
【发布时间】:2013-03-23 03:15:48
【问题描述】:

我创建了一个处理 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;

            }

    }

}

?>

【问题讨论】:

  • 你在哪里使用它,你可能会再次重新初始化类??如果不是,请尝试调试以查看获取 (lastInstertedId) 的值是否设置正确(echo 语句)。

标签: php mysql pdo


【解决方案1】:

你可以像这样添加一个方法:

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();

【讨论】:

  • 超级,成功了 :) 但我想知道你为什么把 $name = NULL 作为参数?这会在哪里发挥作用?
  • 来自lastInsertID。例如,如果您阅读那里的注释,PostgreSQL 需要 $name 参数作为序列对象。
  • @Mike。正如我所说,它只是PDO::lastInsertId() 的包装。检查我链接的文档并注意 Jon 的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
相关资源
最近更新 更多