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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PDO::rollBack – Rolling Back a Transaction in PHP

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 */
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendtransactiondatabasePHProllbackPDO
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.