0% found this document useful (0 votes)
88 views4 pages

Mysqli Begin Tramsaction

This document summarizes the mysqli::begin_transaction and mysqli_begin_transaction functions in PHP for starting a database transaction. It begins a transaction using the InnoDB storage engine and accepts optional flags to define the transaction access mode (read-only or read-write). The function returns true on success or false on failure and requires a live database connection. Examples demonstrate how to begin a transaction in object-oriented and procedural styles.

Uploaded by

Irwan Fath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views4 pages

Mysqli Begin Tramsaction

This document summarizes the mysqli::begin_transaction and mysqli_begin_transaction functions in PHP for starting a database transaction. It begins a transaction using the InnoDB storage engine and accepts optional flags to define the transaction access mode (read-only or read-write). The function returns true on success or false on failure and requires a live database connection. Examples demonstrate how to begin a transaction in object-oriented and procedural styles.

Uploaded by

Irwan Fath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

mysqli::begin_transaction

mysqli_begin_transaction
(PHP 5 >= 5.5.0, PHP 7)
mysqli::begin_transaction -- mysqli_begin_transaction — Starts a transaction

Description ¶
Object oriented style (method):
public mysqli::begin_transaction ([ int $flags = 0 [, string $name ]] ) : bool

Procedural style:
mysqli_begin_transaction ( mysqli $link [, int $flags = 0 [, string $name ]] ) : bool

Begins a transaction. Requires 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". Requires MySQL 5.6 and above.
• MYSQLI_TRANS_START_READ_WRITE: Start the transaction as "START
TRANSACTION READ WRITE". Requires MySQL 5.6 and above.
• MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT: Start the transaction
as "START TRANSACTION WITH CONSISTENT SNAPSHOT".
name

Savepoint name for the transaction.

Return Values ¶
Returns TRUE on success or FALSE on failure.

Examples ¶
Example #1 $mysqli->begin_transaction() example
Object oriented style
<?php
$mysqli = new mysqli("127.0.0.1", "my_user", "my_password", "sakil
a");

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
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "sak
ila");

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() - Turns on or off auto-committing database modifications
• mysqli_commit() - Commits the current transaction
• mysqli_rollback() - Rolls back current transaction

add a note
User Contributed Notes 3 notes

up
down
4
Luc ¶
2 years ago
For PHP<5.5:

mysqli_query($db, "START TRANSACTION");

up
down
2
Ral ¶
1 year ago
If you receive errors like: "This server version doesn't support
'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required" with
versions of MariaDB that DO support them, this is due to an
internal check in mysqli conflicting with a hack in MariaDB to
allow replication with oracle mysql.

MariaDB prefixes its server version numbers with "5.5.5-" for


example "5.5.5-10.3.7-MariaDB-1:10.3.7+maria~stretch". This is
because oracle mysql would interpet the "10" as version 1. Mysql
clients aware of MariaDB have been updated to detect and strip
this prefix.

However the check for mysqli.begin-transaction sees the 5.5.5


prefix and so fails.

The workaround is to specify a custom version string without the


prefix for MariaDB on the command line using the --version option.
Then mysqli.begin-transaction functions as expected.

up
down
0
VasK@hapir ¶
5 months ago
The above answer from Ral worked for us, Thanks a lot. This is how
we implemented the proposed workaround for

Warning: mysqli_begin_transaction(): This server version doesn't


support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required

We appended the following line to /etc/my.cnf and restarted MySQL


server

version=10.2.19-MariaDB

You might also like