Php + mysqli transaction control for bank transfer instances, mysqli bank transfer _ PHP Tutorial

Source: Internet
Author: User
Php + mysqli transaction control implements bank transfer instances and mysqli bank transfers. Php + mysqli transaction control implement bank transfer instance. mysqli bank transfer this article describes how php + mysqli transaction control implement bank transfer. Share it with you for your reference. Specifically, php + mysqli transaction control is used to implement bank transfer instances and mysqli bank transfers

This article describes how to implement bank transfers using php + mysqli transaction control. Share it with you for your reference. The specific analysis is as follows:

Transaction control, that is, all statements are submitted after successful execution. Otherwise, if a previous statement is successfully executed but not later, it is rolled back to the status before execution. This application is illustrated through bank transfer cases. If one account transfers the money, the other account must transfer the money.

The code is as follows:

<? Php // 1. create a database connection object $ mysqli = new MySQLi ("localhost", "root", "123456", "liuyan"); if ($ mysqli-> connect_error) {die ($ mysqli-> connect_error) ;}$ mysqli-> query ("set names 'gbk'"); $ mysqli-> autocommit (false ); // first set autocommit to false, that is, do not automatically submit $ sql1 = "update account set balance = balance-2 where id = 1 ;"; $ sql2 = "update account set balance = balance + 2 where id = 2;"; $ res1 = $ mysqli-> query ($ sql1) or die ($ mysqli-> error); $ res2 = $ Mysqli-> query ($ sql2) or die ($ mysqli-> error); if (! $ Res1 |! $ Res2) {echo "transfer failed"; $ mysqli-> rollback (); // if one fails, roll back} else {$ mysqli-> commit (); // if both statements are successfully executed, submit echo "transfer successful" ;}?>

I hope this article will help you with php programming.

Examples in this article describes how to implement bank transfers using php + mysqli transaction control. Share it with you for your reference. Details...

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.