mysqli::rollback

(PHP 5, PHP 7)
Rolls back current transaction
bool mysqli::rollback ([ int $flags [, string $name ]] )

Object oriented style

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.

Returns:

Returns TRUE on success or FALSE on failure.

Changelog:
5.5.0

Added flags and name parameters.

Examples:
mysqli::rollback() example

Object oriented style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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($link, FALSE);
 
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:

mysqli_begin_transaction() -

mysqli_commit() -

mysqli_autocommit() -

mysqli_release_savepoint() -

doc_php
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.