How does PHP use transactional processing to pay? This paper mainly introduces the method of using transaction processing payment transfer problem in PHP, and analyzes the technique of submitting and rollback of PHP processing transaction. We hope to help you.
In this paper, we describe the method of Php+mysqli using transaction processing transfer problem. Share to everyone for reference
<?php Header ("content-type:text/html; Charset=utf-8 "); $mysqli = new Mysqli ("localhost", "root", "064319", "PHP"); $mysqli->set_charset ("UTF8"); if ($mysqli->connect_errno) {die (' Database connection failed '. $mysqli->connect_error); } $mysqli->autocommit (FALSE); The autocommit mode is set to False $flag = true;//A flag that the transaction was executed successfully $query = "Update account set balance=balance-1000 where id=3"; $result = $mysqli->query ($query); $affected _count = $mysqli->affected_rows; if (!result | | $affected _count = = 0) {//failed $flag = false; } $query = "Update account set balance=balance+1000 where id=2"; $result = $mysqli->query ($query); $affected _count = $mysqli->affected_rows; if (! $result | | $affected _count = = 0) { $flag = false; } if ($flag) { $mysqli->commit (); Echo ' transfer succeeded '; } else { $mysqli->rollback (); Echo ' transfer failed '; } $mysqli->autocommit (TRUE); Reset transaction to Auto-commit $mysqli->close ();?>
Code two:
<?php try{$pdo =new PDO ("Mysql:host=localhost;dbname=psp", "Root", ""); $pdo->exec ("Set names UTF8"); $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);//Set exception handling mode $pdo->setattribute (pdo::attr_ autocommit,0);//Turn off autocommit}catch (pdoexception $e) {echo "database connection failed"; Exit } try{$age = 10; $pdo->begintransaction ();//Start transaction $affected _rows1= $pdo->exec ("Update kfry set k_age=k_age+{$age} where K_name= ' User1 ' "); $affected _rows2= $pdo->exec ("Update kfry set k_age=k_age-{$age} where k_name= ' User2 '");//arbitrarily change to make it perform successfully or fail */if ($ affected_rows1&& $affected _rows2) {$pdo->commit (); Echo "Operation succeeded"; }else{$pdo->rollback (); } */if (! $affected _rows1) throw new Pdoexception ("Add error"); if (! $affected _rows2) throw new Pdoexception ("reduce error"); Echo "Operation succeeded"; $pdo->commit ();//If execution succeeds to the previous two update SQL statements, the entire transaction executes successfully}catch (Pdoexception $e) {echo "Operation failed:". $e->getmessage (); $pdo->rollback ();//The statement in the execution transaction is out of the question, the whole transaction is revoked} $pdo->setattribute (pdo::attr_autocommit,1);Whether the test was successful echo "\ nthe operation result is: \ n"; $sql = "SELECT * from Kfry"; $result = $pdo->query ($sql); foreach ($result as $v) {echo $v [' K_name ']. " ". $v [' K_age ']." \ n "; }?>
Related recommendations:
A detailed explanation of PHP transaction processing
PHP Transaction Data Implementation Code _php tutorial
mysql-thinkphp Transaction Nesting Solution