PHP Example of Transaction Operations Using pdo

  • 2021-11-01 02:24:44
  • OfStack

This article illustrates how PHP uses pdo to implement transaction processing operations. Share it for your reference, as follows:

Benefits of using transactions:

For example, bank user A transfers 100 yuan to user B. This operation is divided into two steps:

(1) The account balance of A has been deducted by 100%.

(2) The account balance of B increased by 100%.

If transactions are not used, assuming that (1) execution succeeds and (2) fails, user B does not receive the receivables and user A suffers in vain.

After using a transaction, no matter which step (1) or (2) fails, it can be rolled back, that is, the account balances of both parties are restored to the previous state.

Not all databases provide transaction support. For example, the Myisam engine in mysql does not support transactions, but the innoDB engine used by default in the new version provides transaction support. (RDBMS mostly supports transactions, while NoSQL Database 1 generally does not.)

Let's look at the specific code:


<?php
$dsn   = "mysql:dbname=pdo;host=localhost";
$user   = "root";
$password = "root";
$dbh   = new PDO($dsn, $user, $password);
// Before using transactions, turn off automatic commit. If you don't close it, you can't roll back when there is an exception. 
// According to the manual, ATTR_AUTOCOMMIT Property is only available in the mysql , OCI(oracle),firebird3 Available in the database 
$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);
$cash = 100;
try {
  $dbh->beginTransaction();
  // Users A Account deduction 100
  $sqlcmd    = "update transaction set useraccount=useraccount - {$cash} where username ='A'";
  $affected_rows = $dbh->exec($sqlcmd);
  if ($affected_rows > 0) {
    echo " Users A Account deduction is successful " . "<br>";
  } else {
    throw new Exception(" Users A Account deduction failed ");
  }
  // Users B Account increase 100
  $affected_rows = $dbh->exec("update transaction set useraccount=useraccount+{$cash} where username ='B'");
  if ($affected_rows > 0) {
    echo " Users B Successful account increase " . "<br>";
  } else {
    throw new Exception(" Users B Account addition failed ");
  }
  echo " Successful transfer ";
  // If the first two steps are successful, the transaction is committed 
  $dbh->commit();
}
catch (PDOException $e) // If an exception occurs in the previous two steps, rollback 
  {
  echo $e->getMessage();
  $dbh->rollback();
}
// After the use of things is over, restart automatic submission 
$dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);
?>

For more readers interested in PHP related contents, please check the topics on this site: "Summary of PHP Database Operation Skills Based on pdo", "Summary of php+Oracle Database Programming Skills", "Encyclopedia of PHP+MongoDB Database Operation Skills", "Introduction to php Object-Oriented Programming", "Introduction to php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: