Php uses transactions to handle transfers

Source: Internet
Author: User
This article mainly introduces php's method of using transaction to handle the transfer problem. The example analyzes php's transaction commit and rollback skills. For more information, see

This article mainly introduces php's method of using transaction to handle the transfer problem. The example analyzes php's transaction commit and rollback skills. For more information, see

This example describes how php + Mysqli processes the transfer problem using transactions. Share with you for your 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); // set the automatic submission mode to false $ flag = true; // indicates whether the transaction is successfully executed. $ query = "update account set balance = balance-1000 where id = 3"; $ result = $ mysqli-> query ($ query ); $ affected_count = $ mysql I-> 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 successful ';} else {$ mysqli-> rollback (); echo 'transfer failed';} $ mysqli-> autocommit (true ); // reset the transaction to automatic commit $ mysqli-> close ();?>

Code 2:

<? 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 the exception handling mode $ pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 0); // disable automatic submission} catch (PDOException $ e) {echo "database connection failed "; exit;} try {$ age = 10; $ pdo-> beginTransaction (); // start the 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 '"); // modify it to make the execution successful or fail at will/* if ($ affected_rows1 & $ affected_rows2) {$ pdo-> commit (); echo "operation successful ";} else {$ pdo-> rollback ();} */if (! $ Affected_rows1) throw new PDOException ("add error"); if (! $ Affected_rows2) throw new PDOException ("reduce error"); echo "operation successful"; $ pdo-> commit (); // if the first two updated SQL statements are successfully executed, the entire transaction is successfully executed} catch (PDOException $ e) {echo "operation failed :". $ e-> getMessage (); $ pdo-> rollback (); // An error occurred while executing the statement in the transaction. The entire transaction is canceled.} $ pdo-> setAttribute (PDO :: ATTR_AUTOCOMMIT, 1); // test whether echo is successful "\ n operation result: \ n"; $ SQL = "select * from kfry "; $ result = $ pdo-> query ($ SQL); foreach ($ result as $ v) {echo $ v ['k _ name']. "". $ v ['k _ age']. "\ n "; }?>

The above is all the content of this article. I hope you will like it.

,

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.