Example 1 — Commit Two Related Updates
Begin, update twice, 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 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
- begin_transaction() opens the unit of work.
- Two UPDATEs move 25 units between accounts.
- mysqli_commit() saves both changes together.
- Sample output confirms a successful commit.

