PHP mysqli_commit() Tutorial 🎯

beginner
11 min

PHP mysqli_commit() Tutorial 🎯

Welcome to our comprehensive guide on PHP's mysqli_commit() function! In this tutorial, we will explore how to manage transactions in MySQL using PHP's mysqli extension. This tutorial is suitable for both beginners and intermediate learners, so let's dive right in!

What is a Transaction in PHP? πŸ“

In the context of databases, a transaction is a series of SQL operations that are executed as a single unit. Transactions help maintain data consistency and integrity by ensuring that either all operations are executed successfully or none of them are.

Why Use Transactions? πŸ’‘

Transactions are crucial in situations where multiple SQL operations need to be performed as a single atomic action. For example, transferring funds between two bank accounts:

  1. Withdraw money from account A.
  2. Deposit money into account B.

If either operation fails, the transaction as a whole should fail, and the bank's data integrity should be preserved.

Introducing mysqli_commit() πŸ“

The mysqli_commit() function is used to commit a transaction in MySQL using PHP's mysqli extension. It ensures that all the SQL operations within the active transaction are executed successfully and are persisted to the database.

How to Use mysqli_commit() πŸ’‘

Let's walk through an example of using mysqli_commit() to create a simple bank transfer system:

php
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "bankdb"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Start a transaction $conn->begin_transaction(); // Withdraw money from account A $sql_withdraw = "UPDATE accounts SET balance = balance - 100 WHERE account_number = 12345"; if (!$conn->query($sql_withdraw)) { $conn->rollback(); echo "Withdrawal failed"; exit; } // Deposit money into account B $sql_deposit = "UPDATE accounts SET balance = balance + 100 WHERE account_number = 67890"; if (!$conn->query($sql_deposit)) { $conn->rollback(); echo "Deposit failed"; exit; } // Commit the transaction $conn->commit(); echo "Transfer successful"; // Close connection $conn->close(); ?>

In this example, we create a transaction, perform two SQL operations (withdraw and deposit), and use mysqli_commit() to commit the transaction if both operations are successful. If either operation fails, we rollback the transaction using mysqli_rollback() and display an error message.

Quiz 🎯

Quick Quiz
Question 1 of 1

What function is used to commit a transaction in MySQL using PHP's `mysqli` extension?

That's it for this comprehensive guide on PHP's mysqli_commit() function! Practice this concept by creating your own transaction-based PHP scripts, and don't forget to check out other helpful tutorials on CodeYourCraft to further enhance your programming skills. Happy coding! πŸ’‘πŸŽ―πŸŒŸ