PHP mysqli_commit() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_commit() permanently saves the current transaction on a MySQLi connection.

Object-oriented style uses $mysqli->commit($flags, $name). Call it after begin_transaction() (or after turning autocommit off) when every step succeeded. Optional flags and name match advanced MySQL commit options.

Syntax

Call mysqli_commit() with the values you need. Start with this line.

PHP
mysqli_commit($mysqli);
CallWhat it does
mysqli_commit($mysqli)Commits the open transaction with default options.
$mysqli->commit()Object-oriented commit.
mysqli_commit($mysqli, 0, "order_tx")Commits with an optional transaction name.

If any statement failed, call rollback() instead of commit(). After commit, autocommit behavior returns to normal unless you keep managing transactions yourself.

Library declarations
PHP
mysqli_commit(mysqli $mysql, int $flags = 0, ?string $name = null): bool

Parameters

ParameterDescription
mysqlRequired. The mysqli connection with an open transaction.
flagsOptional. Commit flags (usually 0 for beginners).
nameOptional. Transaction name, or null.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Commit Two Related Updates

Begin, update twice, 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 accounts SET balance = balance - 25 WHERE id = 1");
mysqli_query($mysqli, "UPDATE accounts SET balance = balance + 25 WHERE id = 2");

if (mysqli_commit($mysqli)) {
    echo "Committed.", PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. begin_transaction() opens the unit of work.
  2. Two UPDATEs move 25 units between accounts.
  3. mysqli_commit() saves both changes together.
  4. Sample output confirms a successful commit.

Example 2 — Object Style Commit

Use the method form after a single insert.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$mysqli->begin_transaction();
$mysqli->query("INSERT INTO audit_log(message) VALUES ('nightly job')");
$mysqli->commit();
echo "saved", PHP_EOL;
$mysqli->close();

How It Works

  1. OO begin_transaction() starts the transaction.
  2. One INSERT runs inside it.
  3. commit() persists the row.
  4. The script prints saved after success.

Example 3 — Check Commit Return Value

Always test whether commit succeeded before telling the user it is done.

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");

if (!mysqli_commit($mysqli)) {
    echo "Commit failed: ", mysqli_error($mysqli), PHP_EOL;
} else {
    echo "Stock updated.", PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. A stock UPDATE runs in a transaction.
  2. mysqli_commit() returns true or false.
  3. On failure you would print mysqli_error().
  4. The demo path prints Stock updated.

Frequently Asked Questions

It permanently saves all statements from the current open transaction on that connection.
Call $mysqli->commit($flags, $name). Beginners usually call $mysqli->commit() with no extra arguments.
Depending on settings and disconnect timing, the server may roll the transaction back. Always commit or roll back explicitly.
With autocommit on, each statement is already committed. commit() matters after you disable autocommit or call begin_transaction().
No. Call rollback(), inspect the error, and decide how to retry or report the problem.

Next: mysqli_connect()

mysqli_connect() opens a new connection to a MySQL server and returns a mysqli object on s.

mysqli_connect() 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