PHP mysqli_rollback() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_rollback() undoes the current transaction so none of its uncommitted changes stay on the server.

With an optional savepoint name it can roll back only to that marker instead of the whole transaction. It pairs with begin_transaction(), commit(), savepoint(), and release_savepoint(). Turn autocommit off (or begin explicitly) before you rely on rollback.

Syntax

Call mysqli_rollback() with the values you need. Start with this line.

PHP
$ok = mysqli_rollback($mysql);
CallWhat it does
mysqli_rollback($link)Cancels the open transaction and discards its uncommitted changes.
mysqli_rollback($link, 0, 'sp1')Rolls back to savepoint sp1 when the server supports it.
$mysqli->rollback()Object-oriented form for a full transaction rollback.

If autocommit is on and you never began a transaction, rollback has nothing useful to undo. Prefer begin_transaction() for clear boundaries. After a failed statement, check errno/error/sqlstate, then decide whether to rollback.

Library declarations
PHP
mysqli_rollback(mysqli $mysql, int $flags = 0, ?string $name = null): bool

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli connection.
flagsOptional. Transaction flags (often 0 for a simple rollback).
nameOptional. Savepoint name to roll back to, or null for a full rollback.

Return Value

Return ValueDescription
trueRollback succeeded.
falseRollback failed.

PHP Examples

Example 1 — Full Transaction Rollback

Demo status lines for begin → change → rollback.

PHP
<?php
echo 'begin_transaction: ok', PHP_EOL;
echo 'update rows: staged', PHP_EOL;
echo 'rollback: ok', PHP_EOL;
echo 'final: no lasting changes', PHP_EOL;

How It Works

  1. Begin a transaction so changes stay pending.
  2. Run your writes (with prepare/bind in real code).
  3. mysqli_rollback() discards those pending changes.
  4. Sample output when connected to a demo database shows the table back to its prior data.

Example 2 — Rollback to a Savepoint

Keep earlier work, undo only the part after a named marker.

PHP
<?php
$flow = [
    'savepoint sp1',
    'bad step',
    "rollback to 'sp1'",
    'commit remaining',
];
foreach ($flow as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. Create savepoint sp1 after the good steps.
  2. If a later step fails, roll back to sp1.
  3. Earlier work before sp1 can still be committed.
  4. release_savepoint() would only drop the name, not undo the bad step.

Example 3 — Check the Boolean Result

Always test the return value in teaching code.

PHP
<?php
$ok = true; // stand-in for mysqli_rollback($link)
echo $ok ? 'rolled back' : 'rollback failed', PHP_EOL;

How It Works

  1. mysqli_rollback() returns true or false.
  2. On false, read mysqli_error() or sqlstate().
  3. OO form is $mysqli->rollback($flags, $name).
  4. Pair every begin with either commit or rollback.

Frequently Asked Questions

It undoes an open transaction, or rolls back to an optional savepoint name.
When a step in a multi-query unit of work fails and you do not want any of the pending changes kept.
Yes. Pass the savepoint name (and usually flags 0) so only work after that marker is undone.
commit() keeps the transaction changes; rollback() discards them. Use one ending for each begun transaction.
You need an open transaction. With autocommit on and no begin_transaction(), each statement may already be final.

Next: mysqli_savepoint()

mysqli_savepoint() creates a named marker inside the current transaction so you can roll b.

mysqli_savepoint() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful