Content · Web Development · PHP

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

Commits all changes in the current transaction. Calling commit outside an active transaction throws PDOException in modern

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

Commits all changes in the current transaction.

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

What It Does


Commits all changes in the current transaction.

Syntax and Return Value


PDO::commit(): mixed

The return value and exceptional cases depend on the call described here: Calling commit outside an active transaction throws PDOException in modern PHP.

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->beginTransaction();
$pdo->exec("UPDATE products SET price = price + 48 WHERE id = 1");
$pdo->commit();
echo $pdo->inTransaction() ? "active" : "committed";

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->beginTransaction();
$pdo->exec("UPDATE products SET price = price + 62 WHERE id = 2");
$pdo->commit();
echo $pdo->inTransaction() ? "active" : "committed";

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->beginTransaction();
$pdo->exec("UPDATE products SET price = price + 79 WHERE id = 3");
$pdo->commit();
echo $pdo->inTransaction() ? "active" : "committed";

Important Behavior and Edge Cases


Calling commit outside an active transaction throws PDOException in modern PHP.

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

Comparison with Related Approaches


ApproachImportant distinction
PDO::commitCommits all changes in the current transaction.
Related API / patternrollBack undoes changes still pending in the active transaction.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesCalling commit outside an active transaction throws PDOException in modern PHP.
Using a related API interchangeablyrollBack undoes changes still pending in the active transaction.

Key Takeaways


  • Commits all changes in the current transaction.
  • Calling commit outside an active transaction throws PDOException in modern PHP.
  • rollBack undoes changes still pending in the active transaction.

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.