In MySQL, the COMMIT statement is used to save all changes made during the current transaction. Once a COMMIT is executed, the changes can no longer be undone. Transactions allow you to execute a sequence of operations as a single unit to ensure data integrity.
Here's an example of how to use COMMIT in MySQL:
<?php
// Connect to the database
$conn = new mysqli('localhost', 'username', 'password', 'database');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Start a transaction
$conn->begin_transaction();
try {
// Execute some SQL statements
$conn->query("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')");
$conn->query("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')");
// Commit the transaction
$conn->commit();
echo "Transaction committed successfully.";
} catch (Exception $e) {
// Rollback the transaction if an error occurs
$conn->rollback();
echo "Transaction rolled back: " . $e->getMessage();
}
// Close the connection
$conn->close();
?>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?