db2_last_insert_id

(PECL ibm_db2 >= 1.7.1)

db2_last_insert_idReturns the auto generated ID of the last insert query that successfully executed on this connection

说明

db2_last_insert_id ( resource $resource ) : string

Returns the auto generated ID of the last insert query that successfully executed on this connection.

The result of this function is not affected by any of the following:

  • A single row INSERT statement with a VALUES clause for a table without an identity column.

  • A multiple row INSERT statement with a VALUES clause.

  • An INSERT statement with a fullselect.

  • A ROLLBACK TO SAVEPOINT statement.

参数

resource

A valid connection resource as returned from db2_connect() or db2_pconnect(). The value of this parameter cannot be a statement resource or result set resource.

返回值

Returns the auto generated ID of last insert query that successfully executed on this connection.

范例

Example #1 A db2_last_insert_id() example

The following example shows how to return the auto generated ID of last insert query that successfully executed on this connection.

<?php

$database 
"SAMPLE";
$user "db2inst1";
$password "ibmdb2";

$conn db2_connect($database$user$password);
if(
$conn) {
    
$createTable "CREATE TABLE lastInsertID 
      (id integer GENERATED BY DEFAULT AS IDENTITY, name varchar(20))"
;
    
$insertTable "INSERT INTO lastInsertID (name) VALUES ('Temp Name')";

    
$stmt = @db2_exec($conn$createTable);

    
/* Checking for single row inserted. */
    
$stmt db2_exec($conn$insertTable);
    
$ret =  db2_last_insert_id($conn);
    if(
$ret) {
        echo 
"Last Insert ID is : " $ret "\n";
    } else {
        echo 
"No Last insert ID.\n";
    }
    
    
db2_close($conn);
}
else {
    echo 
"Connection failed.";
}
?>

以上例程会输出:

Last Insert ID is : 1

相关文章
php cubrid 函数 return the id generated for the last updated auto increment columnphp ibm db2 函数 returns or sets the autocommit state for a database connectionphp ibm db2 函数 returns an object with properties that describe the db2 database clientphp ibm db2 函数 returns a string containing the sqlstate returned by the last connection attemptphp ibm db2 函数 returns the last connection error message and sqlcode valuephp ibm db2 函数 returns a connection to a databasephp ibm db2 函数 returns an array indexed by column name representing a row in a result setphp ibm db2 函数 returns an array indexed by both column name and position representing a row in a result setphp ibm db2 函数 returns an object with properties representing columns in the fetched rowphp ibm db2 函数 returns the auto generated id of the last insert query that successfully executed on this connectionphp ibm db2 函数 returns the number of fields contained in a result setphp ibm db2 函数 returns a persistent connection to a databasephp ibm db2 函数 returns an object with properties that describe the db2 database serverphp ibm db2 函数 returns a string containing the sqlstate returned by an sql statementphp ibm db2 函数 returns a string containing the last sql statement error messagephp ibm db2 函数 returns a result set listing the tables and associated privileges in a databasephp maxdb 函数 returns the auto generated id used in the last queryphp maxdb 函数 returns the thread id for the current connectionphp sqlsrv 函数 returns the number of rows modified by the last insert update or delete query executedphp mysqlnduhconnection returns the auto generated id used in the last query
关注编程学问公众号