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