Manuel PHP

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

Description

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.

Liste de paramètres

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.

Valeurs de retour

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

Exemples

Exemple #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.

  1. <?php
  2.  
  3. $database = "SAMPLE";  
  4. $user = "db2inst1";  
  5. $password = "ibmdb2";  
  6.  
  7. $conn = db2_connect($database, $user, $password);  
  8. if($conn) { 
  9.    $createTable = "CREATE TABLE lastInsertID
  10.     (id integer GENERATED BY DEFAULT AS IDENTITY, name varchar(20))"; 
  11.    $insertTable = "INSERT INTO lastInsertID (name) VALUES ('Temp Name')"; 
  12.  
  13.    $stmt = @db2_exec($conn, $createTable); 
  14.  
  15.    /* Checking for single row inserted. */ 
  16.    $stmt = db2_exec($conn, $insertTable); 
  17.    $ret = db2_last_insert_id($conn); 
  18.    if($ret) { 
  19.       echo "Last Insert ID is : " . $ret . "\n"; 
  20.    } else { 
  21.       echo "No Last insert ID.\n"; 
  22.    } 
  23.    
  24.    db2_close($conn);  
  25. }  
  26. else { 
  27.    echo "Connection failed.";  
  28. }  
  29. ?> 

L'exemple ci-dessus va afficher :

Last Insert ID is : 1


Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique