Home PHP MySQLi mysqli_begin_transaction() PHP mysqli_begin_transaction() Function Definition In PHP, mysqli_begin_transaction() starts a database transaction on a MySQLi connection so several statements can succeed or fail together.
Object-oriented style uses $mysqli->begin_transaction($flags, $name). Optional flags mark the transaction as read-only, read-write, or with a consistent snapshot. An optional name creates a named transaction or savepoint-style label depending on the server.
Syntax Call mysqli_begin_transaction() with the values you need. Start with this line.
mysqli_begin_transaction($mysqli); Call What it does mysqli_begin_transaction($mysqli)Starts a normal transaction with default flags. mysqli_begin_transaction($mysqli, MYSQLI_TRANS_START_READ_ONLY)Starts a read-only transaction when the server supports it. $mysqli->begin_transaction(0, "order_tx")Object-oriented start with an optional name.
Requires a transactional storage engine such as InnoDB. After begin_transaction(), call commit() on success or rollback() on failure.
Library declarations mysqli_begin_transaction(mysqli $mysql, int $flags = 0, ?string $name = null): bool Parameters Parameter Description mysql Required. The mysqli connection object. flags Optional. 0 or a MYSQLI_TRANS_START_* constant such as MYSQLI_TRANS_START_READ_ONLY. name Optional. A transaction name, or null for none.
Return Value Return Value Description bool true on success, false on failure.
PHP Examples 1. Start and Commit a Transaction 2. Object Style With Rollback 3. Read-Only Transaction Flag Example 1 — Start and Commit a Transaction Begin a transaction, run one write, then commit. Sample output when connected to a demo database.
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
mysqli_begin_transaction($mysqli);
mysqli_query($mysqli, "UPDATE products SET stock = stock - 1 WHERE id = 10");
mysqli_commit($mysqli);
echo "Transaction committed.", PHP_EOL;
mysqli_close($mysqli); How It Works mysqli_begin_transaction() starts the unit of work. One UPDATE runs inside the open transaction. mysqli_commit() saves the change permanently. The sample message confirms the happy path. Example 2 — Object Style With Rollback Start with the method form and roll back when a check fails.
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->begin_transaction();
$mysqli->query("UPDATE products SET stock = stock - 5 WHERE id = 10");
$ok = false; // pretend a later check failed
if (!$ok) {
$mysqli->rollback();
echo "Rolled back.", PHP_EOL;
}
$mysqli->close(); How It Works begin_transaction() opens the transaction in OO style. An UPDATE runs, but a later check is treated as failed. rollback() undoes the UPDATE. Sample output shows the cancel path instead of commit. Frequently Asked Questions What does mysqli_begin_transaction() do? It starts a transaction so several SQL statements are treated as one unit. You finish with commit to save or rollback to undo.
What is the OO form? Call $mysqli->begin_transaction($flags, $name). Flags and name are optional, just like the procedural function.
Which flags can I pass? Common ones are MYSQLI_TRANS_START_READ_ONLY, MYSQLI_TRANS_START_READ_WRITE and MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT. Use 0 for the default.
Do I need InnoDB? Yes for real rollback support. Non-transactional engines will not undo earlier statements when you call rollback().
How is this different from mysqli_autocommit(false)? Both can start manual transaction mode. begin_transaction() is the clearer, modern way to open a transaction and can take flags and a name.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion