PDO::lastInsertId
Returns the identifier for the row most recently inserted into a table in the database. The table must have an IDENTITY NOT NULL column. If a sequence name is provided, lastInsertId
returns the most recently inserted sequence number for the provided sequence name (for more information about sequence numbers, see here).
Syntax
string PDO::lastInsertId ([ $name = NULL ] );
Parameters
$name: An optional string that lets you specify a sequence name.
Return Value
If no sequence name is provided, a string of the identifier for the row most recently added. If a sequence name is provided, a string of the identifier for the sequence most recently added. If the method call fails, empty string is returned.
Remarks
Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.
Between version 2.0 and 4.3, the optional parameter is a table name, and the return value is the ID of the row most recently added to the provided table.
Starting with 5.0, the optional parameter is regarded as a sequence name, and the return value is the sequence most recently added for the provided sequence name.
If a table name is provided for versions after 4.3, lastInsertId
returns an empty string.
Sequences are supported only in SQL Server 2012 and above.
Example
<?php
$server = "myserver";
$databaseName = "mydatabase";
$uid = "myusername";
$pwd = "mypasword";
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$databaseName", $uid, $pwd);
// One sequence, two tables
$tableName1 = 'seqtable1';
$tableName2 = 'seqtable2';
$sequenceName = 'sequence1';
$stmt = $conn->query("IF OBJECT_ID('$sequenceName', 'SO') IS NOT NULL DROP SEQUENCE $sequenceName");
$sql = "CREATE TABLE $tableName1 (seqnum INTEGER NOT NULL PRIMARY KEY, SomeNumber INT)";
$stmt = $conn->query($sql);
$sql = "CREATE TABLE $tableName2 (ID INT IDENTITY(1,2), SomeValue char(10))";
$stmt = $conn->query($sql);
$sql = "CREATE SEQUENCE $sequenceName AS INTEGER START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 100 CYCLE";
$stmt = $conn->query($sql);
$ret = $conn->exec("INSERT INTO $tableName1 VALUES( NEXT VALUE FOR $sequenceName, 20)");
$ret = $conn->exec("INSERT INTO $tableName1 VALUES( NEXT VALUE FOR $sequenceName, 40)");
$ret = $conn->exec("INSERT INTO $tableName1 VALUES( NEXT VALUE FOR $sequenceName, 60)");
$ret = $conn->exec("INSERT INTO $tableName2 VALUES( '20' )");
// return the last sequence number if sequence name is provided
$lastSeq1 = $conn->lastInsertId($sequenceName);
// defaults to $tableName2 -- because it returns the last inserted id value
$lastRow = $conn->lastInsertId();
// providing a table name in lastInsertId should return an empty string
$lastSeq2 = $conn->lastInsertId($tableName2);
echo "Last sequence number = $lastSeq1\n";
echo "Last inserted ID = $lastRow\n";
echo "Last inserted ID when a table name is supplied = $lastSeq2\n";
// One table, two sequences
$tableName = 'seqtable';
$sequence1 = 'sequence1';
$sequence2 = 'sequenceNeg1';
$stmt = $conn->query("IF OBJECT_ID('$sequence1', 'SO') IS NOT NULL DROP SEQUENCE $sequence1");
$stmt = $conn->query("IF OBJECT_ID('$sequence2', 'SO') IS NOT NULL DROP SEQUENCE $sequence2");
$sql = "CREATE TABLE $tableName (ID INT IDENTITY(1,1), SeqNumInc INTEGER NOT NULL PRIMARY KEY, SomeNumber INT)";
$stmt = $conn->query($sql);
$sql = "CREATE SEQUENCE $sequence1 AS INTEGER START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 100";
$stmt = $conn->query($sql);
$sql = "CREATE SEQUENCE $sequence2 AS INTEGER START WITH 200 INCREMENT BY -1 MINVALUE 101 MAXVALUE 200";
$stmt = $conn->query($sql);
$ret = $conn->exec("INSERT INTO $tableName VALUES( NEXT VALUE FOR $sequence1, 20 )");
$ret = $conn->exec("INSERT INTO $tableName VALUES( NEXT VALUE FOR $sequence2, 180 )");
$ret = $conn->exec("INSERT INTO $tableName VALUES( NEXT VALUE FOR $sequence1, 40 )");
$ret = $conn->exec("INSERT INTO $tableName VALUES( NEXT VALUE FOR $sequence2, 160 )");
$ret = $conn->exec("INSERT INTO $tableName VALUES( NEXT VALUE FOR $sequence1, 60 )");
$ret = $conn->exec("INSERT INTO $tableName VALUES( NEXT VALUE FOR $sequence2, 140 )");
// return the last sequence number of 'sequence1'
$lastSeq1 = $conn->lastInsertId($sequence1);
// return the last sequence number of 'sequenceNeg1'
$lastSeq2 = $conn->lastInsertId($sequence2);
// providing a table name in lastInsertId should return an empty string
$lastSeq3 = $conn->lastInsertId($tableName);
echo "Last sequence number of sequence1 = $lastSeq1\n";
echo "Last sequence number of sequenceNeg1 = $lastSeq2\n";
echo "Last sequence number when a table name is supplied = $lastSeq3\n";
$stmt = $conn->query("DROP TABLE $tableName1");
$stmt = $conn->query("DROP TABLE $tableName2");
$stmt = $conn->query("DROP SEQUENCE $sequenceName");
$stmt = $conn->query("DROP TABLE $tableName");
$stmt = $conn->query("DROP SEQUENCE $sequence1");
$stmt = $conn->query("DROP SEQUENCE $sequence2");
unset($stmt);
unset($conn);
} catch (Exception $e) {
echo "Exception $e\n";
}
?>
The expected output is:
Last sequence number = 3
Last inserted ID = 1
Last inserted ID when a table name is supplied =
Last sequence number of sequence1 = 3
Last sequence number of sequenceNeg1 = 198
Last sequence number when a table name is supplied =