PHP mysqli_autocommit() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_autocommit() turns automatic commit on or off for the current MySQL connection.

When autocommit is on (the default), every statement is saved right away. When you turn it off, you group several statements into a transaction and finish with commit or rollback. Object-oriented style uses $mysqli->autocommit($enable).

Syntax

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

PHP
mysqli_autocommit($mysqli, false);
CallWhat it does
mysqli_autocommit($mysqli, false)Turns autocommit off so you can start a manual transaction.
mysqli_autocommit($mysqli, true)Turns autocommit back on.
$mysqli->autocommit(false)Object-oriented form of the same call.

You can check the current setting with SQL: SELECT @@autocommit. Turning autocommit off is often paired with begin_transaction(), commit() and rollback().

Library declarations
PHP
mysqli_autocommit(mysqli $mysql, bool $enable): bool

Parameters

ParameterDescription
mysqlRequired. The mysqli connection object.
enableRequired. true to turn autocommit on, false to turn it off.

Return Value

Return ValueDescription
booltrue on success, false on failure.

PHP Examples

Example 1 — Turn Autocommit Off

Disable autocommit before a short group of writes. Sample output when connected to a demo database.

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

if (mysqli_autocommit($mysqli, false)) {
    echo "Autocommit is off.", PHP_EOL;
}

mysqli_close($mysqli);

How It Works

  1. Connect with placeholder demo credentials.
  2. mysqli_autocommit($mysqli, false) disables automatic commit.
  3. The function returns true when the setting is accepted.
  4. Later you would commit or roll back, then close the connection.

Example 2 — Object Style Autocommit Toggle

Use the method form, then turn autocommit back on.

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

$mysqli->autocommit(false);
echo "manual mode", PHP_EOL;

$mysqli->autocommit(true);
echo "auto mode", PHP_EOL;

$mysqli->close();

How It Works

  1. new mysqli() opens the connection.
  2. autocommit(false) starts manual transaction mode.
  3. autocommit(true) restores the default automatic commits.
  4. Sample messages show both settings were applied in order.

Example 3 — Pair Autocommit With Commit

Turn autocommit off, run two updates, then commit once.

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

mysqli_query($mysqli, "UPDATE accounts SET balance = balance - 10 WHERE id = 1");
mysqli_query($mysqli, "UPDATE accounts SET balance = balance + 10 WHERE id = 2");

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

mysqli_autocommit($mysqli, true);
mysqli_close($mysqli);

How It Works

  1. Autocommit is turned off so both UPDATEs stay in one transaction.
  2. The two balance changes form a simple transfer.
  3. mysqli_commit() saves both changes together.
  4. Autocommit is turned back on before closing.

Frequently Asked Questions

It turns MySQL automatic commit on or off for one connection. Off means you control when a group of statements is saved with commit or cancelled with rollback.
Yes. Each successful statement is committed immediately unless you disable autocommit or start an explicit transaction.
Run SELECT @@autocommit and read the result. 1 means on, 0 means off.
Often yes for clarity. You can start with begin_transaction() after turning autocommit off, or use begin_transaction() with flags. The important part is finishing with commit or rollback.
Call $mysqli->autocommit(true) or $mysqli->autocommit(false). The behavior matches the procedural function.

Next: mysqli_begin_transaction()

mysqli_begin_transaction() starts a database transaction on a MySQLi connection so several.

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