The mysqli_commit
function in PHP is used to commit a transaction in MySQL. It is typically used after a series of queries have been executed within a transaction to ensure that the changes made by those queries are saved to the database. A commit makes the changes permanent and visible to other transactions.
Key Points:
- Transaction: In MySQL, a transaction is a sequence of SQL queries that execute as a single unit. It is used to ensure that either all the queries in the transaction are executed successfully, or none of them are (in case of an error, you can roll back the transaction).
mysqli_commit
: This function commits the current transaction, meaning that all changes made since the last commit or rollback are saved to the database.
Syntax:
bool mysqli_commit(mysqli $link);
- $link: The MySQL connection link identifier. This is the connection you used to establish the connection to the MySQL server.
- Returns
TRUE
on success orFALSE
on failure.
Example of Using mysqli_commit
:
Let's go through an example that shows how mysqli_commit
works in a typical transaction.
<?php
// Step 1: Create a connection
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check for connection errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Step 2: Start a transaction
$mysqli->begin_transaction();
try {
// Step 3: Execute multiple queries inside the transaction
$mysqli->query("UPDATE accounts SET balance = balance - 100 WHERE user_id = 1");
$mysqli->query("UPDATE accounts SET balance = balance + 100 WHERE user_id = 2");
// Step 4: Commit the transaction if everything is successful
$mysqli->commit();
echo "Transaction successfully committed!";
} catch (Exception $e) {
// Step 5: Rollback the transaction in case of error
$mysqli->rollback();
echo "Transaction failed: " . $e->getMessage();
}
// Step 6: Close the connection
$mysqli->close();
?>
Explanation:
- Connection: A connection is established to the MySQL server using
new mysqli()
. - Begin Transaction:
begin_transaction()
is called to start the transaction. - Queries: Inside the transaction, two update queries are executed to transfer money between two users.
- Commit: If both queries are executed successfully,
mysqli_commit()
is called to make the changes permanent. - Error Handling: If an error occurs during the transaction (e.g., a query fails), the transaction is rolled back using
mysqli->rollback()
, which undoes all the changes made so far. - Close: After committing or rolling back, the connection is closed with
mysqli->close()
.
When to Use mysqli_commit
:
- Use
mysqli_commit()
when you are executing multiple SQL queries that need to be treated as a single transaction. - It ensures that the changes are persistent and will not be reverted unless a rollback is explicitly called.
- It is important to always handle exceptions (or errors) with a rollback mechanism to prevent partial changes from being committed.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.