简体   繁体   English

使用PHP-PDO记录查询时如何获取最后的插入ID

[英]How to get last Insert Id while logging the query using PHP-PDO

Recently I have been creating a function to record every changes in the database, simply I record it to a table. 最近,我一直在创建一个函数来记录数据库中的所有更改,只需将其记录到表中即可。

Table backup_log: backupTableName, backupLastUpdate, backupLastupload 表backup_log:backupTableName,backupLastUpdate,backupLastupload

So, every time changed, its will record the date to "backup_log". 因此,每次更改时,它将日期记录到“ backup_log”。 It's runs well, but I have problem when I need Last Insert Id, I using $orderid = $dbh->lastInsertId('orderId'); 它运行良好,但是当我需要最后插入ID时遇到问题,我使用$ orderid = $dbh->lastInsertId('orderId'); to get "last insert Id", but its return "0". 获取“最后插入ID”,但返回“ 0”。 I figure out that the problem comes when I put querylog() function that runs another query while the query() function still also run. 我弄清楚,问题就来了,当我把querylog()函数运行另一个查询,而query()函数仍然还运行。 coz its using same PDO object. 因为使用相同的PDO对象。 So, any suggestion for me to able to get "last insert id", while runs querylog() ? 那么,有什么建议让我在运行querylog()时能够获得“最后插入ID”?

This is my code: 这是我的代码:

//$dbh is object of PDO connection

$sql = query("INSER INTO orders ...");
$orderid = $dbh->lastInsertId('orderId'); // table "orders"

function query($sql){
    #this function is not the full version, just for example
    global $dbh;
    $query=$dbh->exec($sql);

    querylog($sql); // Log every insert, delete and update query
}   

function querylog($sql){  
    global $dbh; // PDO object
    global $now;

    //Table backup_log: backupTable, backupLastUpdate, backupLastupload

    ###### Log: for insert and update ########
    #if the query too long, cut for 200 chars and convert them to array
    $sql_clean_space=str_replace(array("  ",'`'),array(" ",''),$sql); //change double space to single space
    if(strlen($sql_clean_space) > 200){
        $sql_clean_space = substr($sql_clean_space, 0, 200); // get only 200 char
    }

    $sql_array=explode(" ", $sql_clean_space); //convert to array to get the command name, insert or update
    $now_date=$now;

    #save log
    if(strtolower($sql_array[0])=='insert' or strtolower($sql_array[0])=='delete'){
        #check whether the query is insert or update
        $sqlx = "SELECT * FROM backup_log WHERE backupTable='".$sql_array[2]."'";
        $query=$dbh->query($sqlx);  
        $check_row=$query->rowCount($query);

        if($check_row){
            $sql_log="UPDATE backup_log SET backupLastUpdate='$now' WHERE backupTable='". $sql_array[2] . "'";
        }else{
            $sql_log="INSERT INTO backup_log VALUES('".$sql_array[2]."', '$now_date','$now_date')";
        } 
    }elseif(strtolower($sql_array[0])=='update'){
        $sqlx = "SELECT * FROM backup_log WHERE backupTable='".$sql_array[1]."'";
        $query=$dbh->query($sqlx);  
        $check_row=$query->rowCount($query);

        if($check_row){ 
            $sql_log="UPDATE backup_log SET backupLastUpdate='$now' WHERE backupTable='". $sql_array[1] . "'";
            }else{
            $sql_log="INSERT INTO backup_log VALUES('".$sql_array[1]."', '$now_date','$now_date')";
        }
    }
    $query_log=$dbh->exec($sql_log);
    ####### End of log ######################
}

Maybe you can assign last insert id in query functions and it can be preserved while query executions. 也许您可以在查询函数中分配最后一个插入ID,并且可以在执行查询时保留该ID。 See my code below; 请参阅下面的代码;

$sql = query("INSER INTO orders ...", 'orderId'); //$sql is equal to last insert id 
$orderid = $sql

function query($sql, $specific_id_column){
    #this function is not the full version, just for example
    global $dbh;
    $query=$dbh->exec($sql);
    $last_id = $dbh->lastInsertId($specific_id_column);
    querylog($sql); // Log every insert, delete and update query
    return $last_id;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM