The mysqli_rollback()
function in PHP is used to undo (rollback) a transaction that has been started using the mysqli_begin_transaction()
function. When you perform multiple queries within a transaction, and something goes wrong, calling mysqli_rollback()
will revert all changes made during the transaction and ensure data consistency.
Syntax:
mysqli_rollback(connection);
- connection: The MySQL connection object returned by
mysqli_connect()
.
Example Usage:
-
Starting a Transaction: To begin a transaction, you use
mysqli_begin_transaction()
before running any queries. If an error occurs during the queries, you can callmysqli_rollback()
to undo everything. -
Rollback in Case of Error: If an error occurs during any query execution, you can rollback the changes to keep your data in a consistent state.
Example:
<?php
// Create a connection to the MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check if the connection was successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Start the transaction
$mysqli->begin_transaction();
try {
// Query 1: Insert into table1
$sql1 = "INSERT INTO table1 (name, email) VALUES ('John Doe', 'john@example.com')";
if (!$mysqli->query($sql1)) {
throw new Exception("Error in Query 1: " . $mysqli->error);
}
// Query 2: Insert into table2
$sql2 = "INSERT INTO table2 (user_id, product) VALUES (LAST_INSERT_ID(), 'Product A')";
if (!$mysqli->query($sql2)) {
throw new Exception("Error in Query 2: " . $mysqli->error);
}
// If both queries succeed, commit the transaction
$mysqli->commit();
echo "Transaction successful!";
} catch (Exception $e) {
// An error occurred, so rollback the transaction
$mysqli->rollback();
echo "Transaction failed: " . $e->getMessage();
}
// Close the connection
$mysqli->close();
?>
Explanation:
$mysqli->begin_transaction()
: Starts the transaction.- Query 1: Attempts to insert data into
table1
. - Query 2: Attempts to insert data into
table2
usingLAST_INSERT_ID()
to reference the last inserted ID fromtable1
. - If any query fails (for example, if there’s an issue with the second insert), an exception is thrown.
$mysqli->rollback()
: If an exception is caught, the changes are rolled back to the state beforebegin_transaction()
.$mysqli->commit()
: If all queries succeed, the changes are committed to the database.
When to Use:
- You use
mysqli_rollback()
when you need to ensure that a group of database queries are either fully committed or fully rolled back. - This is useful when you are performing related database operations, and partial changes could result in data inconsistency.
Key Points:
- Commit: When everything goes as expected, you call
mysqli_commit()
to apply the changes. - Rollback: If something goes wrong during the transaction, use
mysqli_rollback()
to revert the changes.
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.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.