How do you use COMMIT with an example?

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(); ?>

MySQL COMMIT transactions SQL database data integrity