mysqli::rollback

mysqli_rollback

(PHP 5)

mysqli::rollback -- mysqli_rollbackRolls back current transaction

Description

Object oriented style

bool mysqli::rollback ([ int $flags [, string $name ]] )

Procedural style

bool mysqli_rollback ( mysqli $link [, int $flags [, string $name ]] )

Rollbacks the current transaction for the database.

Parameters

link

Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()

flags

A bitmask of MYSQLI_TRANS_COR_* constants.

name

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

Return Values

Returns TRUE on success or FALSE on failure.

Changelog

Version Description
5.5.0 Added flags and name parameters.

Examples

Example #1 mysqli::rollback() example

Object oriented style

<?php
$mysqli 
= new mysqli("localhost""my_user""my_password""world");

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

/* disable autocommit */
$mysqli->autocommit(FALSE);

$mysqli->query("CREATE TABLE myCity LIKE City");
$mysqli->query("ALTER TABLE myCity Type=InnoDB");
$mysqli->query("INSERT INTO myCity SELECT * FROM City LIMIT 50");

/* commit insert */
$mysqli->commit();

/* delete all rows */
$mysqli->query("DELETE FROM myCity");

if (
$result $mysqli->query("SELECT COUNT(*) FROM myCity")) {
    
$row $result->fetch_row();
    
printf("%d rows in table myCity.\n"$row[0]);
    
/* Free result */
    
$result->close();
}

/* Rollback */
$mysqli->rollback();

if (
$result $mysqli->query("SELECT COUNT(*) FROM myCity")) {
    
$row $result->fetch_row();
    
printf("%d rows in table myCity (after rollback).\n"$row[0]);
    
/* Free result */
    
$result->close();
}

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

$mysqli->close();
?>

Procedural style

<?php
$link 
mysqli_connect("localhost""my_user""my_password""world");

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

/* disable autocommit */
mysqli_autocommit($linkFALSE);

mysqli_query($link"CREATE TABLE myCity LIKE City");
mysqli_query($link"ALTER TABLE myCity Type=InnoDB");
mysqli_query($link"INSERT INTO myCity SELECT * FROM City LIMIT 50");

/* commit insert */
mysqli_commit($link);

/* delete all rows */
mysqli_query($link"DELETE FROM myCity");

if (
$result mysqli_query($link"SELECT COUNT(*) FROM myCity")) {
    
$row mysqli_fetch_row($result);
    
printf("%d rows in table myCity.\n"$row[0]);
    
/* Free result */
    
mysqli_free_result($result);
}

/* Rollback */
mysqli_rollback($link);

if (
$result mysqli_query($link"SELECT COUNT(*) FROM myCity")) {
    
$row mysqli_fetch_row($result);
    
printf("%d rows in table myCity (after rollback).\n"$row[0]);
    
/* Free result */
    
mysqli_free_result($result);
}

/* Drop table myCity */
mysqli_query($link"DROP TABLE myCity");

mysqli_close($link);
?>

The above examples will output:

0 rows in table myCity.
50 rows in table myCity (after rollback).

See Also

Коментарии

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:

<?php
$all_query_ok
=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();

$mysqli->close();
?>

hope to be helpful!
2009-02-10 08:31:00
http://php5.kiev.ua/manual/ru/mysqli.rollback.html
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.
2009-07-22 19:08:17
http://php5.kiev.ua/manual/ru/mysqli.rollback.html
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.

Example:
<?php
$mysqli 
= new mysqli("localhost""gugbageri""gugbageri""gugbageri");

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

/* disable autocommit */
$mysqli->autocommit(FALSE);

/* 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 */
$mysqli->commit();

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

/* we commit the inserted row */
$mysqli->commit();

/* 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 */
$mysqli->rollback();

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

/* we commit the inserted row */
$mysqli->commit();

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

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

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

$mysqli->close();
?>

This will output:
Id: 1.
Id: 5.
2010-03-10 05:04:36
http://php5.kiev.ua/manual/ru/mysqli.rollback.html
Автор:
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
2012-02-10 23:16:44
http://php5.kiev.ua/manual/ru/mysqli.rollback.html
Автор:
If you use savepoints  - eg savepoint($foo) - be wary of trying to rollback to the save point with rollback(0, $foo) as that executes "ROLLBACK /* $foo */" instead of "ROLLBACK TO `$foo`". 

The manual page is clear about this, but is easily overlooked.

Instead use: $mysqli->query("ROLLBACK TO `$foo`");
2023-09-07 20:55:39
http://php5.kiev.ua/manual/ru/mysqli.rollback.html

    Поддержать сайт на родительском проекте КГБ