Mysql transaction processing (a transfer program)-PHP-PHP source code

Source: Internet
Author: User
Mysql transaction processing (a transfer program)-PHP code

 ", Mysqli_connect_error (); exit () ;}$ success = TRUE; $ price = 8000; $ result = $ mysqli-> query ("select cash from account where name = 'usera '"); 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 name = 'usera '");} else {echo 'insufficient balance '; exit ();} if (! $ Result or $ mysqli-> affected_rows! = 1) {$ success = FALSE;} $ result = $ mysqli-> query ("UPDATE account set cash = cash + $ price where name = 'userb '"); 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 ();?> & Create table account {userID smallint unsigned not null auto_increment, name varchar (45) not null, cash decimal (2000) not null, primary key (userID) type = InnoDB; insert into account (name, cash) values ('usera ', '123 '); insert into account (name, cash) values ('userb', '123 ');

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.