PHP mysqli_begin_transaction() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

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.

PHP
mysqli_begin_transaction($mysqli);
CallWhat 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
PHP
mysqli_begin_transaction(mysqli $mysql, int $flags = 0, ?string $name = null): bool

Parameters

ParameterDescription
mysqlRequired. The mysqli connection object.
flagsOptional. 0 or a MYSQLI_TRANS_START_* constant such as MYSQLI_TRANS_START_READ_ONLY.
nameOptional. A transaction name, or null for none.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Start and Commit a Transaction

Begin a transaction, run one write, then commit. Sample output when connected to a demo database.

PHP
<?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

  1. mysqli_begin_transaction() starts the unit of work.
  2. One UPDATE runs inside the open transaction.
  3. mysqli_commit() saves the change permanently.
  4. 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
<?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

  1. begin_transaction() opens the transaction in OO style.
  2. An UPDATE runs, but a later check is treated as failed.
  3. rollback() undoes the UPDATE.
  4. Sample output shows the cancel path instead of commit.

Example 3 — Read-Only Transaction Flag

Start a read-only transaction when you only need consistent reads.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");

mysqli_begin_transaction($mysqli, MYSQLI_TRANS_START_READ_ONLY);
$result = mysqli_query($mysqli, "SELECT COUNT(*) AS c FROM products");
$row = mysqli_fetch_assoc($result);
echo "Products: ", $row["c"], PHP_EOL;
mysqli_commit($mysqli);

mysqli_close($mysqli);

How It Works

  1. MYSQLI_TRANS_START_READ_ONLY asks for a read-only start when supported.
  2. A SELECT runs inside the transaction.
  3. fetch_assoc() reads the count from the demo result.
  4. commit() ends the read-only transaction cleanly.

Frequently Asked Questions

It starts a transaction so several SQL statements are treated as one unit. You finish with commit to save or rollback to undo.
Call $mysqli->begin_transaction($flags, $name). Flags and name are optional, just like the procedural function.
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.
Yes for real rollback support. Non-transactional engines will not undo earlier statements when you call rollback().
Both can start manual transaction mode. begin_transaction() is the clearer, modern way to open a transaction and can take flags and a name.

Next: mysqli_change_user()

mysqli_change_user() switches the MySQL user (and optional default database) on an already.

mysqli_change_user() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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