PDO::rollBack – Rolling Back a Transaction in PHP
The article explains how the PHP PDO::rollBack() method aborts the current transaction, restores autocommit mode when needed, outlines its parameter‑less signature, return values, and provides a complete code example demonstrating transaction handling and rollback behavior.
PDO::rollBack() is a PHP method that rolls back the current database transaction and, if the database was in autocommit mode, restores autocommit after the rollback.
If the database is set to autocommit, the method will re‑enable it after rolling back. Some databases (e.g., MySQL) perform an implicit commit when DDL statements such as DROP TABLE are executed inside a transaction, which cannot be rolled back.
Parameters
This method takes no parameters.
Return value
Returns TRUE on success or FALSE on failure.
Example
<?php
// Start a transaction and disable autocommit
$dbh->beginTransaction();
/* Change database schema and data */
$sth = $dbh->exec("DROP TABLE fruit");
$sth = $dbh->exec("UPDATE dessert SET name = 'hamburger'");
/* Detect error and roll back changes */
$dbh->rollBack();
/* At this point the connection returns to autocommit mode */
?>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
