mysqli::begin_transaction

(PHP 5 >= 5.5.0, PHP 7)
Starts a transaction
public bool mysqli::begin_transaction ([ int $flags [, string $name ]] )

Object oriented style (method):

Procedural style:

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

Begins a transaction. Requires MySQL 5.6 and above, and the InnoDB engine (it is enabled by default). For additional details about how MySQL transactions work, see » http://dev.mysql.com/doc/mysql/en/commit.html.

Parameters:
link

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

flags

Valid flags are:

  • MYSQLI_TRANS_START_READ_ONLY: Start the transaction as "START TRANSACTION READ ONLY".

  • MYSQLI_TRANS_START_READ_WRITE: Start the transaction as "START TRANSACTION READ WRITE".

  • MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT: Start the transaction as "START TRANSACTION WITH CONSISTENT SNAPSHOT".

name

Savepoint name for the transaction.

Returns:

Returns TRUE on success or FALSE on failure.

Examples:
$mysqli->begin_transaction() example

Object oriented style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$mysqli new mysqli("127.0.0.1""my_user""my_password""sakila");
 
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
}
 
$mysqli->begin_transaction(MYSQLI_TRANS_START_READ_ONLY);
 
$mysqli->query("SELECT first_name, last_name FROM actor");
$mysqli->commit();
 
$mysqli->close();
?>

Procedural style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$link = mysqli_connect("127.0.0.1""my_user""my_password""sakila");
 
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
 
mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_ONLY);
 
mysqli_query($link"SELECT first_name, last_name FROM actor LIMIT 1");
mysqli_commit($link);
 
mysqli_close($link);
?>
See also:

mysqli_autocommit() -

mysqli_commit() -

mysqli_rollback() -

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

Please login to continue.