PDO::lastInsertId
傳回最近在資料庫的資料表中插入之資料列的識別碼。 資料表必須有 IDENTITY NOT NULL 資料行。 如果已提供序列名稱,lastInsertId
會傳回所提供序列名稱最近插入的序號 (如需序號的詳細資訊,請參閱這裡)。
語法
string PDO::lastInsertId ([ $name = NULL ] );
參數
$name:讓您指定序列名稱的選擇性字串。
傳回值
若未提供序列名稱,會傳回最近加入之資料列的識別碼字串。 若已提供序列名稱,會傳回最近加入之序列的識別碼字串。 若方法呼叫失敗,會傳回空字串。
備註
PDO 支援已新增至 Microsoft Drivers for PHP for SQL Server 2.0 版。
在 2.0 版和 4.3 版之間,選擇性參數為資料表名稱,而傳回值則是最近加入所提供資料表的資料列識別碼。
從 5.0 版開始,選擇性參數會被視為序列名稱,而傳回值則是最近針對所提供序列名稱加入的序列。
若針對 4.3 版之後的版本提供資料表名稱,lastInsertId
會傳回空字串。
只有 SQL Server 2012 及更新版本才支援序列。
範例
<?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";
}
?>
預期的輸出為:
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 =