0Pricing
PHP Academy · Lesson

Transactions with PDO

Group multiple queries into atomic transactions with beginTransaction and commit.

Transactions with PDO is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is a Transaction?

A transaction groups multiple SQL statements into a single atomic unit: all succeed (commit) or all roll back.

ACID Properties

Transactions guarantee ACID: Atomicity, Consistency, Isolation, Durability.

beginTransaction()

Start a transaction with $pdo->beginTransaction(). Auto-commit is disabled until you call commit() or rollBack().

commit()

Permanently saves all changes made since beginTransaction().

rollBack()

Discards all changes made since beginTransaction(), reverting the database to its state before the transaction.

Transaction Pattern

Always wrap transactions in try/catch:

<?php
try {
    $pdo->beginTransaction();

    $pdo->prepare("UPDATE accounts SET balance = balance - ? WHERE id = ?")
        ->execute([100, $senderId]);

    $pdo->prepare("UPDATE accounts SET balance = balance + ? WHERE id = ?")
        ->execute([100, $receiverId]);

    $pdo->commit();
} catch (PDOException $e) {
    $pdo->rollBack();
    throw $e;
}

Savepoints

PDO does not support nested transactions natively. Use SQL SAVEPOINT commands for partial rollbacks within a transaction.

inTransaction()

Check whether a transaction is active with $pdo->inTransaction(). Returns true or false.

Auto-commit Mode

By default PDO is in auto-commit mode: every statement is immediately committed. beginTransaction() disables auto-commit.

Isolation Levels

MySQL supports: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ (default), SERIALIZABLE. Set with $pdo->exec("SET TRANSACTION ISOLATION LEVEL READ COMMITTED").

When to Use Transactions

Use for: bank transfers, order placement with inventory deduction, any multi-table write where partial completion is unacceptable.

DDL and Transactions

In MySQL, DDL statements (CREATE TABLE, ALTER TABLE) cause an implicit commit. You cannot roll back DDL inside a transaction.

Quick Check

What does rollBack() do?

Frequently asked questions

Is the “Transactions with PDO” lesson free?

Yes — the full text of “Transactions with PDO” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Transactions with PDO”?

Group multiple queries into atomic transactions with beginTransaction and commit. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Transactions with PDO” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Connecting to MySQL with PDO
  2. Prepared Statements and Binding
  3. Fetching Results with PDO
  4. Transactions with PDO
← Back to PHP Academy