Transactions group related writes so either all succeed or all are rolled back.
The examples focus on the operation itself, including the important distinction: Autocommit commits each statement independently; transactions coordinate several writes.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Before You Start
The examples use PHP 8.2+ features where shown. Database snippets require the corresponding PDO driver, demo tables, and credentials configured outside source code.
Example 1: Basic Use
Rollback compensates only database changes in the active transaction, not emails or network calls.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$pdo->beginTransaction();
try {
$stmt = $pdo->prepare("UPDATE users SET active = 1 WHERE id = ?");
$stmt->execute([3]);
$pdo->commit();
} catch (Throwable $e) {
if ($pdo->inTransaction()) { $pdo->rollBack(); }
throw $e;
}
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Rollback compensates only database changes in the active transaction, not emails or network calls.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$pdo->beginTransaction();
try {
$stmt = $pdo->prepare("UPDATE users SET active = 1 WHERE id = ?");
$stmt->execute([8]);
$pdo->commit();
} catch (Throwable $e) {
if ($pdo->inTransaction()) { $pdo->rollBack(); }
throw $e;
}
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Rollback compensates only database changes in the active transaction, not emails or network calls.
<?php
$pdo = new PDO(
"mysql:host=" . (getenv("DB_HOST") ?: "127.0.0.1") . ";dbname=" . (getenv("DB_NAME") ?: "demo") . ";charset=utf8mb4",
getenv("DB_USER") ?: "app",
getenv("DB_PASSWORD") ?: "",
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false]
);
$pdo->beginTransaction();
try {
$stmt = $pdo->prepare("UPDATE users SET active = 1 WHERE id = ?");
$stmt->execute([21]);
$pdo->commit();
} catch (Throwable $e) {
if ($pdo->inTransaction()) { $pdo->rollBack(); }
throw $e;
}
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
Catch Throwable, rollback on failure, and remember external APIs are not rolled back with the database.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| Adding Database Transactions to a PHP Workflow | Transactions group related writes so either all succeed or all are rolled back. |
| Related API or alternative | Autocommit commits each statement independently; transactions coordinate several writes. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Catch Throwable, rollback on failure, and remember external APIs are not rolled back with the database. |
| Confusing it with a related API | Autocommit commits each statement independently; transactions coordinate several writes. |
Key Takeaways
- Transactions group related writes so either all succeed or all are rolled back.
- Catch Throwable, rollback on failure, and remember external APIs are not rolled back with the database.
- Autocommit commits each statement independently; transactions coordinate several writes.
Use Adding Database Transactions to a PHP Workflow when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.