This article describes how to implement the transfer function of PHP + Mysql based on transaction processing. The instance analyzes the usage skills of mysql transaction processing, which has some reference value. For more information, see
This article describes how to implement the transfer function of PHP + Mysql based on transaction processing. The instance analyzes the usage skills of mysql transaction processing, which has some reference value. For more information, see
This article describes how PHP + Mysql transfers funds based on transaction processing. Share it with you for your reference. The details are as follows:
<? Php header ("Content-Type: text/html; charset = UTF-8"); $ mysqli = new mysqli ("localhost", "root", "", "test "); if (mysqli_connect_errno () {printf ("Connection Failed: % s
", Mysqli_connect_error (); exit () ;}$ success = TRUE; $ price = 8000; $ result = $ mysqli-> query (" select cash from account where "); while ($ row = $ result-> fetch_assoc () {$ value = $ row ["cash"]; echo $ value ;}$ mysqli-> autocommit (0 ); if ($ value >=$ price) {$ result = $ mysqli-> query ("UPDATE account set cash = cash-$ price where ");} else {echo 'balance not enough '; exit ();} if (! $ Result or $ mysqli-> affected_rows! = 1) {$ success = FALSE;} $ result = $ mysqli-> query ("UPDATE account set cash = cash + $ price where"); if (! Result or $ mysqli-> affected_rows! = 1) {$ success = FALSE;} if ($ success) {$ mysqli-> commit (); echo 'transfer successful! ';} Else {$ mysqli-> rollback (); echo "Transfer failed! ";}$ Mysqli-> autocommit (1); $ query =" select cash from account where name =? "; $ Stmt = $ mysqli-> prepare ($ query); $ stmt-> bind_param ('s ', $ name); $ name = 'usera '; $ stmt-> execute (); $ stmt-> store_result (); $ stmt-> bind_result ($ cash); while ($ stmt-> fetch ()) echo "User userA value :". $ cash; $ mysqli-> close ();?>
The Database SQL statement is as follows:
Create table account {userID smallint unsigned not null auto_increment, name varchar (45) not null, cash decimal (9,2) not null, primary key (userID) type = InnoDB; insert into account (name, cash) values ('usera ', '000000'); insert into account (name, cash) values ('userb', '000000 ');
I hope this article will help you with php programming.
,