Php+mysql transaction-based approach to the transfer function, MySQL transaction processing _php tutorial

Source: Internet
Author: User

Php+mysql transaction-based method for transferring functions, MySQL transaction processing


This paper describes the method of Php+mysql transaction-based transfer function. Share to everyone for your reference. Specific 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 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 succeeded! '; }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 is:". $cash; $mysqli->close ();? >

The database SQL statements are as follows:

CREATE TABLE account{userID smallint unsigned not NULL auto_increment, name varchar no null, cash decimal (9,2) not n ull, primary key (UserID)) Type=innodb;insert into account (Name,cash) VALUES (' UserA ', ' # '); INSERT into account (name, Cash) VALUES (' UserB ', ' 10000 ');

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1028963.html www.bkjia.com true http://www.bkjia.com/PHPjc/1028963.html techarticle Php+mysql based on transaction processing to implement the transfer function method, MySQL transaction processing This article describes the Php+mysql based on transaction processing to implement the transfer function method. Share to everyone for your reference ...

  • 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.