PDO
suggest changeVersions
The original version list on this page was PHP 5.0 through 7.1. PDO has shipped with PHP since 5.1.
Introduction
The PDO (PHP Data Objects) extension allows developers to connect to numerous different types of databases and execute queries against them in a uniform, object-oriented manner.
Syntax
PDO::LastInsertId()PDO::LastInsertId($columnName)// some drivers need the column name
Remarks
Warning Do not forget to check for exceptions while using lastInsertId(). PDO throws only when the error mode is PDO::ERRMODE_EXCEPTION, which has been the default since PHP 8.0. The driver can raise:
SQLSTATE IM001 : Driver does not support this function
Check for that exception like this:
// Retrieving the last inserted id
$id = null;
try {
$id = $pdo->lastInsertId(); // return value is a string
}
catch( PDOException $e ) {
echo $e->getMessage();
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents