PHP Doku:: Returns the auto generated ID of the last insert query that successfully executed on this connection - function.db2-last-insert-id.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenIBM DB2, Cloudscape and Apache DerbyIBM DB2 Funktionendb2_last_insert_id

Ein Service von Reinhard Neidl - Webprogrammierung.

IBM DB2 Funktionen

<<db2_get_option

db2_lob_read>>

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

Beschreibung

int db2_last_insert_id ( resource $resource )

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.

Parameter-Liste

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.

Rückgabewerte

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

Beispiele

Beispiel #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.";
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Last Insert ID is : 1


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
Mark Adkins
10.06.2010 18:28
On the iSeries and maybe other db2 platforms, this function is not defined--PHP Fatal error:  Call to undefined function  db2_last_insert_id().

The alternative is to use IDENTITY_VAL_LOCAL().

The above example becomes:

<?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')";
   
$getIdentity = "SELECT IDENTITY_VAL_LOCAL() AS LASTID FROM SYSIBM.SYSDUMMY1";

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

   
/* Checking for single row inserted. */
   
$stmt = db2_exec($conn, $insertTable);

   
$stmt = db2_exec($conn, $getIdentity);
   
$row = db2_fetch_assoc($stmt);
   
$ret = $row['LASTID'];

    if(
$ret) {
        echo
"Last Insert ID is : " . $ret . "\n";
    } else {
        echo
"No Last insert ID.\n";
    }
   
   
db2_close($conn);
}
else {
    echo
"Connection failed.";
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)