Php + Mysqli use transactions to handle transfers. mysqli transaction processing _ PHP Tutorial

Source: Internet
Author: User
Php + Mysqli use transactions to handle transfer problem instances and mysqli transaction processing. Php + Mysqli use transactions to handle transfers. mysqli transaction processing this article describes how php + Mysqli uses transactions to handle transfers. Share it with you for your reference. The actual php + Mysqli uses transactions to handle the transfer problem instance, and mysqli transaction processing

This example describes how php + Mysqli processes the transfer problem using transactions. Share it with you for your reference. The specific implementation method is as follows:

<? 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 ();?>

I hope this article will help you with php programming.

Examples in this article describes how php + Mysqli uses transactions to handle transfers. 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.