PHP transaction transfer problem with transaction processing, PHP transactions Transfer _php Tutorial

Source: Internet
Author: User

PHP transaction transfer problem with transaction processing, PHP transactions transfer


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 "; }?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/987886.html www.bkjia.com true http://www.bkjia.com/PHPjc/987886.html techarticle PHP Transaction Transaction Transfer problem, PHP transactional Transfer This example describes the method by which Php+mysqli uses transaction transfer issues. Share for everyone to refer to the PHP header ("Conten ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.