Content · Web Development · PHP

PHP PDO::lastInsertId() Method: Usage and Examples

Returns the last inserted identifier or sequence value for the connection where supported. Behavior is driver-specific, and PostgreSQL may require a sequence name; read before another

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Returns the last inserted identifier or sequence value for the connection where supported.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Returns the last inserted identifier or sequence value for the connection where supported.

Syntax and Return Value


PDO::lastInsertId(Parameter #0 [ <optional> ?string $name = null ]): mixed

The return value and exceptional cases depend on the call described here: Behavior is driver-specific, and PostgreSQL may require a sequence name; read before another insert.

Example 1: Basic Use


This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$pdo->prepare("INSERT INTO products (name,price,active) VALUES (?, ?, ?)")->execute(["Shelf 1",48,1]);
echo $pdo->lastInsertId();

Example 2: A Second Scenario


This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$pdo->prepare("INSERT INTO products (name,price,active) VALUES (?, ?, ?)")->execute(["Shelf 2",62,1]);
echo $pdo->lastInsertId();

Example 3: Practical Checks


This self-contained SQLite example uses in-memory demo data; it requires the pdo_sqlite driver.

<?php
$pdo = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL, price INTEGER NOT NULL, active INTEGER)');
$pdo->exec("INSERT INTO products (id,name,price,active) VALUES (1,'Desk',120,1),(2,'Lamp',35,1),(3,'Chair',85,0)");
$pdo->prepare("INSERT INTO products (name,price,active) VALUES (?, ?, ?)")->execute(["Shelf 3",79,1]);
echo $pdo->lastInsertId();

Important Behavior and Edge Cases


Behavior is driver-specific, and PostgreSQL may require a sequence name; read before another insert.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
PDO::lastInsertIdReturns the last inserted identifier or sequence value for the connection where supported.
Related API / patternRETURNING can retrieve generated keys directly in databases that support it.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesBehavior is driver-specific, and PostgreSQL may require a sequence name; read before another insert.
Using a related API interchangeablyRETURNING can retrieve generated keys directly in databases that support it.

Key Takeaways


  • Returns the last inserted identifier or sequence value for the connection where supported.
  • Behavior is driver-specific, and PostgreSQL may require a sequence name; read before another insert.
  • RETURNING can retrieve generated keys directly in databases that support it.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.