mixed insert_Id( optional string $tableName = '', optional string $columnName = '' )
insert_Id() returns the last auto-increment number of any table, generated by an insert on a table with an auto-increment column. If no such insertion has occurred or the database does not support the operation, the function returns false.
The value retrieved is connection based, meaning that each concurrent connection (even if multiple connections are associated with the same procedure) has its own instance of the number.
It is important to remember that there is only a single storage instance for the number, and if one insert statement with an auto-increment number succeeds another, the insert_id value for the second statement will overwrite the value of the first, so retrieve the value of the first before executing the second.
/* * Connection assumed */ /* * Insert into table with auto-increment column, current highest value 100 */ $db->execute('INSERT INTO monkeys (type) value "arctic"'); $lastId = $db->insert_Id(); print $lastId; /* * returns value 101 */ /* * Insert into a different table with auto-increment, current highest value 50. */ $db->execute('INSERT INTO pickups (type) value "F150"'); $lastId = $db->insert_Id(); print $lastId; /* * returns value 51, There is now no way of retrieving the previous last id */
If you use the emulated sequence function with the OCI8 driver, you must provide the table name in order to retrieve the last sequence. This is because the value is being returned from a table related sequence, not a global insertId instance.
/* * Oracle Connection assumed */ $sequence = insertId('some_table');
For that reason, the usage rules about a single instance do not apply. However if you are writing cross-database code you should follow those rules.