PHP MySQL Transactions Tutorial: Safe Database Writes
Use PHP MySQL transactions to group related queries and roll them back safely when an error occurs. See commit and rollback in a practical money transfer example.

Php Transaction operation can be thought of as a process used during money transfers in banking transactions, and the importance of this operation is critical. Therefore, if any error occurs during the operation, the operation must be rolled back. We can explain this with an example as follows.
When person A wants to send $500 from their account to person B, $500 will be deducted from person A’s account and $500 will be added to person B’s account. However, if an error occurs after $500 is deducted from person A’s account but before $500 is added to person B’s account, an annoying situation will arise. To prevent this kind of annoying situation, MySQL TRANSACTIONS are used in PHP.
The PHP MYSQL Transaction operation is performed in 3 simple steps.
Starting the transaction “beginTransaction()”
If the operations performed after the transaction starts are successful, committing them “commit()”
If the transaction is not successful and the operation needs to be rolled back “rollBack ()”
As an example, let’s examine our code below.
<?php
// establishing our database connection
$host = "localhost";
$username = "root";
$password = "123456";
$dbname = "testveritabani";
//We connect to the database with the information specified above.
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//We start the Transaction operation we mentioned above here
$conn->beginTransaction();
$isim='Serhat';
$soyisim='Alpaslan';
$eposta='serhat@serhatalpaslan.com';
//We write our insert query with the prepare method, but we use placeholders instead of the actual values
$stmt=$conn->prepare("INSERT INTO isimler (isim, soyisim, email) VALUES (:isim, :soyisim, :email)");
//another way to bind the values
$resultInsert=$stmt->execute([
":isim" => $isim,
":soyisim" => $soyisim,
":email" => $eposta,
]); //if this operation is successful, the result will return as the Boolean value TRUE.
$stmt = $conn->prepare("UPDATE isimler set isim=:isim WHERE id=:id");
$resultUpdate = $stmt->execute([
":isim" => "Serhat",
":id" => "90"
]);
//If all operations are successful
if($resultInsert && $resultUpdate){
//We commit the transaction we started
$conn->commit();
echo "<br>"."Tüm işlemler başarıyla gerçekleşti";
}else {
//The transaction we started is rolled back with the following command.
$conn->rollBack();
echo "<br>"."Tüm işlemler iptal edildi ve geri alındı.";
}
//we fetch data from the database
$query = $conn->prepare("SELECT * FROM isimler");
$query->execute(); //Executes a prepared statement
$result=$query->fetchAll(PDO::FETCH_ASSOC);
?>
In the example above, we prepared a simple example explaining how the php transaction operation is performed. However, of course, it would be wrong to think that php transaction operations are used only in monetary transactions. Therefore, we will prepare something about different usage patterns for php with transactions in the future.
You can find more information about Php Transaction at this address. You can also check out the other php lessons.
Share it if it helped