This article mainly introduces php's method of using transactions to handle transfers. The example analyzes php's skills for transaction commit and rollback, for more information about how php + Mysqli uses transactions to handle transfers, see the example in this article. 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 = $ 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 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.