(PHP 5, PHP 7, PHP 8)

mysqli::rollback -- mysqli_rollbackRolls back current transaction


Stile orientato agli oggetti

public mysqli::rollback(int $flags = 0, ?string $name = null): bool

Stile procedurale

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

Rollbacks the current transaction for the database.

Elenco dei parametri


Solo nello stile procedurale: un identificatore restituito da mysqli_connect() o mysqli_init()


A bitmask of MYSQLI_TRANS_COR_* constants.


If provided then ROLLBACK/*name*/ is executed.

Valori restituiti

Restituisce true in caso di successo, false in caso di fallimento.

Log delle modifiche

Versione Descrizione
8.0.0 name is now nullable.


See the mysqli::begin_transaction() example.



This function does not work with non transactional table types (like MyISAM or ISAM).

Vedere anche:

add a note add a note

User Contributed Notes 4 notes

Steven McCoy
10 years ago
Remember that MyISAM tables do not support rollbacks.

I just drove myself crazy for an afternoon trying to figure out what was wrong with my code - meanwhile it was fine all along
Lorenzo - webmaster AT 4tour DOT it
13 years ago
This is an example to explain the powerful of the rollback and commit functions.
Let's suppose you want to be sure that all queries have to be executed without errors before writing data on the database.
Here's the code:

=true; // our control variable

//we make 4 inserts, the last one generates an error
//if at least one query returns an error we change our control variable
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (200)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (300)") ? null : $all_query_ok=false;
$mysqli->query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; //duplicated PRIMARY KEY VALUE

//now let's test our control variable
$all_query_ok ? $mysqli->commit() : $mysqli->rollback();


hope to be helpful!
xcalibur at xcalibur dot dk
12 years ago
Just a note about auto incremental ids and rollback.
When using transactions and inserting into a table containing a column with auto incremental ids, the id will be incremented even though the transaction is rolled back.

This might occupy a lot of ids if a lot of rollbacks are performed.

= new mysqli("localhost", "gugbageri", "gugbageri", "gugbageri");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());

/* disable autocommit */

/* We just create a test table with one auto incremental primary column and a content column*/
$mysqli->query("CREATE TABLE TestTable ( `id_column` INT NOT NULL  AUTO_INCREMENT , `content` INT NOT NULL , PRIMARY KEY ( `id_column` )) ENGINE = InnoDB;");

/* commit newly created table */

/* we insert a row */
$mysqli->query("INSERT INTO TestTable (content) VALUES (99)");

/* we commit the inserted row */

/* we insert another three rows */
$mysqli->query("INSERT INTO TestTable (content) VALUES (99)");
$mysqli->query("INSERT INTO TestTable (content) VALUES (99)");
$mysqli->query("INSERT INTO TestTable (content) VALUES (99)");

/* we the rollback */

/* we insert a row */
$mysqli->query("INSERT INTO TestTable (content) VALUES (99)");

/* we commit the inserted row */

if (
$result = $mysqli->query("SELECT id_column FROM TestTable")) {

$row = $result->fetch_row()) {
printf("Id: %d.\n", $row[0]);
/* Free result */

/* Drop table TestTable */
$mysqli->query("DROP TABLE TestTable");


This will output:
Id: 1.
Id: 5.
jd at dilltree dot com
13 years ago
Something to consider when using transact is that you should not perform a normal query on the same table (such as a DELETE) immediately after a transaction.  If the transaction rolls-back, the DELETE will execute and even show affected rows, but the row can be magically re-inserted even if the rollback() command comes before the DELETE query.
To Top