Home > Backend Development > PHP Tutorial > Parsing php mysql transaction processing rollback operation (with examples)_PHP tutorial

Parsing php mysql transaction processing rollback operation (with examples)_PHP tutorial

WBOY
Release: 2016-07-21 15:00:15
Original
954 people have browsed it

Many novices will encounter such a situation during the project process, such as: in the forum currency deduction project, if the user suddenly disconnects from the Internet, computer crashes, power outage, and other natural disasters when paying forum coins, resulting in This transaction was unsuccessful (that is, the user's coins have been deducted, but there are no consumption records in the server database, etc.). How should this situation be handled?

At this time, we can use Mysql transaction rollback to process it. How to write the code?
Then let me talk about how to handle this mysql transaction rollback.

First of all, only INNODB and BDB type data tables in MYSQL can support transaction processing! Other types are not supported!

What should we do if our data table already exists and is not of the two types mentioned above?

1. I can find a software called MySQL-Front, which can change the table type.
2. We can also change it through SQL statements. The sql statements can be written like this:

Copy code The code is as follows:

ALTER TABLE TABLENAME type = InnoDB;

After all changes to the tables that need to be modified are completed, we can go to the PHP file and use the code to test.

Copy code The code is as follows:

mysql_query("BEGIN"); //or mysql_query("START TRANSACTION");

$sql = "INSERT INTO ...";
$sql2 = "INSERT INTO ...";
$res = mysql_query($sql);
$res1 = mysql_query($sql2 );
if($res && $res1){
mysql_query("COMMIT");
echo 'Submission successful. ';
}else{
mysql_query("ROLLBACK");
echo 'Data rollback. ';
}

mysql_query("END");


After seeing this, you should know how to use PHP to handle mysql transaction rollback. It's very simple!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328083.htmlTechArticleMany novices will encounter such a situation during the project process, such as: in the forum currency deduction project, If the user is suddenly disconnected from the Internet, computer crashes, power outage, etc. when paying forum coins...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template