- skip to content

# Database Abstraction Layer

### User Tools

- Login

### Site Tools

- Recent changes
- Media Manager
- Sitemap

### Sidebar

##### Resources

- Reference Manual
- User Guide
- The Data Dictionary
- ADOdb-xmlschema
- ADOdb Active Record
- Performance Monitoring
- Date/Time Library
- Session Management Library
- Unsupported Addons
- Coding Standards

##### Supported Databases

- Access
- ADO
- CSV
- Firebird
- Frontbase
- IBM DB2
- Informix
- Interbase
- LDAP
- Microsoft SQL Server
- MySQL
- Netezza
- ODBC
- ODBTP
- Oracle
- PDO
- Postgresql
- SAP ADS
- SAP ASE
- SAP Maxdb
- SAP Sqlanywhere
- Sqlite
- Sybase
- Plain Text
- Visual Foxpro

##### Other

- Feature Comparison Matrix
- Connection Matrix
- Include File Guide

v5:reference:connection:insert_id

# insert_Id

##### Syntax

    mixed insert_Id(
    optional string $tableName = '',
    optional string $columnName = ''
    )

## Description

The function `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.

## Postgresql / SAP MaxDB

The only drivers that require the optional `$table` and `$column` arguments are postgresql and sap_maxdb as they support table and column specific insert id numbers.

## Usage

    /*
     * 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
     */

v5/reference/connection/insert_id.txt · Last modified: 2016/01/16 15:15 by mnewnham

### Page Tools

- Show pagesource
- Old revisions
- Backlinks
- Rename Page
- Back to top