How can I return LastInsertID from PDO whin a method of a class

前提是你 提交于 2019-12-24 16:34:06

问题


I have create a small class that handles my MySql queries.

However, Now I am trying to get the value of the last inserted id but it is not wokring for me

if my processQuery method in the class I do a prepare statement with any query like insert/update/remove

I have added this line $this->lastInsertId = $this->pdo->lastInsertId; which should give me the last inserted Id and stores it in the public variable called $lastInsertId then I can access it from my code outside.

How can I get the last inserted ID to work with this class??

Thanks

this is my class

<?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:


You can add a method like this:

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

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

It is just a wrapper around PDO::lastInsertId(). You won't need a local copy of the last insert id. If PDO isn't connected it will throw an Exception. You can change this to return FALSE; if this fit's your design more.

Use it like this:

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


来源:https://stackoverflow.com/questions/15753753/how-can-i-return-lastinsertid-from-pdo-whin-a-method-of-a-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!