db2_last_insert_id
(PECL ibm_db2 >= 1.7.1)
db2_last_insert_id — Returns the auto generated ID of the last insert query that successfully executed on this connection
Описание
$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.
Список параметров
-
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.
Примеры
Пример #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 Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- IBM DB2, Cloudscape and Apache Derby
- db2_autocommit
- db2_bind_param
- db2_client_info
- db2_close
- db2_column_privileges
- db2_columns
- db2_commit
- db2_conn_error
- db2_conn_errormsg
- db2_connect
- db2_cursor_type
- db2_escape_string
- db2_exec
- db2_execute
- db2_fetch_array
- db2_fetch_assoc
- db2_fetch_both
- db2_fetch_object
- db2_fetch_row
- db2_field_display_size
- db2_field_name
- db2_field_num
- db2_field_precision
- db2_field_scale
- db2_field_type
- db2_field_width
- db2_foreign_keys
- db2_free_result
- db2_free_stmt
- db2_get_option
- db2_last_insert_id
- db2_lob_read
- db2_next_result
- db2_num_fields
- db2_num_rows
- db2_pclose
- db2_pconnect
- db2_prepare
- db2_primary_keys
- db2_procedure_columns
- db2_procedures
- db2_result
- db2_rollback
- db2_server_info
- db2_set_option
- db2_special_columns
- db2_statistics
- db2_stmt_error
- db2_stmt_errormsg
- db2_table_privileges
- db2_tables
Коментарии
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.";
}
?>