Home > Article > Backend Development > Example of transaction processing transfer problem using php+Mysqli_PHP tutorial
This article mainly introduces the method of php+Mysqli using transaction processing to handle transfer problems. The example analyzes how php+mysqli handles transactions. For tips on submission and rollback, friends who need it can refer to it
The example in this article describes how php+Mysqli uses transactions to handle transfer issues. Share it with everyone for your reference. The specific implementation method is as follows:
?
3 4 513 14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<🎜>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 automatic commit mode to false $flag = true; //Flag indicating whether the transaction was 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 automatically commit $mysqli->close(); ?> |