Home > Backend Development > PHP Problem > How to delete transaction in php

How to delete transaction in php

藏色散人
Release: 2023-03-17 14:20:01
Original
1458 people have browsed it

PHP transaction deletion operation method: 1. Use the "autocommit(false)" method to turn off automatic submission of the database; 2. When all operation statements are successful, submit to the database through "commit()". If the operation If it fails, use the "rollback()" method to roll back the deletion.

How to delete transaction in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to delete transaction in php?

php Transaction processing transaction

MySQL transactions are mainly used to process data with large operations and high complexity. For example, in the personnel management system, if you delete a person, you need to delete the basic information of the person, and also delete the information related to the person, such as mailbox, articles, etc. In this way, these database operation statements constitute a transaction. !

In MySQL, only databases or tables that use the Innodb database engine support transactions

Transaction processing can be used to maintain the integrity of the database and ensure that batches of SQL statements are either fully executed or Do not execute all

Transactions are used to manage insert, update, and delete statements

Generally speaking, transactions must meet four conditions (ACID): Atomicity (atomicity), Consistency (stability) ), Isolation (isolation), Durability (persistence)

1. Atomicity of transactions: a set of transactions either succeeds or is withdrawn.

2. Stability: If there is illegal data (foreign key constraints and the like), the transaction will be withdrawn.

3. Isolation: transactions run independently. If the result of one transaction affects other transactions, the other transactions will be withdrawn. 100% isolation of transactions requires sacrificing speed.

4. Durability: Once a transaction is committed, its changes to the data in the database are permanent. Even if the database fails, it should not have any impact.

Execute the following program

$mysqli = new mysqli('localhost','root','mayi1991','mysqldemo');
if($mysqli->connect_error){
    die('数据库连接错误'.$mysqli->connect_error);
}
$sql1 = "update account set balance = balance - 2 where id = 1";
//这里故意写错指令中的balance1属性
$sql2 = "update account set balance1 = balance + 2 where id = 2";        
$result1 = $mysqli->query($sql1);
$result2 = $mysqli->query($sql2);
if(!$result1 || !$result2){
    die('操作错误'.$mysqli->error);
}else{
    die('操作成功');
}
$mysqli->close();
Copy after login

Although the above code reports an error, in the database, the balance with id=1 has changed; this will cause problems;

We want to change at the same time. If there is an error, it will not change; at this time, we need "transaction control" to ensure "consistency";

We The method that needs to be used is autocommit() commit(); look at the code below

$mysqli = new mysqli('localhost','root','mayi1991','mysqldemo');
if($mysqli->connect_error){
    die('数据库连接错误'.$mysqli->connect_error);
}
//关闭数据库自动提交
$mysqli->autocommit(false);
$sql1 = "update account set balance = balance - 2 where id = 1";
//这里故意写错属性balance1
$sql2 = "update account set balance1 = balance + 2 where id = 2";
$result1 = $mysqli->query($sql1);
$result2 = $mysqli->query($sql2);
if(!$result1 || !$result2){
    die('操作错误'.$mysqli->error);
    $mysqli->rollback();    //事务回退
}else{
    //操作全部正确后再提交
    $mysqli->commit();
}
$mysqli->close();
Copy after login

First use the autocommit(false) method to turn off automatic submission of the database, and then when all operation statements are successful, commit() is submitted to the database ;

If the operation fails, we use the rollback() method to roll back.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to delete transaction in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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