<img src="https://img.php.cn/upload/image/523/551/786/1619489169179091.jpg" title="1619489169179091.jpg" alt="Detailed explanation of rollBack() method in PHP">
## Connecting to the database in PHP is a relatively daily operation, but the data in the database is generally more important. In order to avoid misoperation, the data The integrity of it is destroyed, so we used the
rollBack() method to avoid some misoperations. This article will take you to take a look at
PDO::rollBack.
PDO::rollBack ( )
PDO::beginTransaction() affairs. If no transaction is active, a PDOException will be thrown.
true on success, or
false on failure.
1. Connect to the database:
<?php $servername="localhost"; $username="root"; $password="root123456"; $dbname="my_database"; $pdo=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password); echo "连接成功"."<br>"; $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);
输出:连接成功
2. Data operation
//开启一个事务 $pdo->beginTransaction(); $sql="drop table tp_user"; $stat = $pdo->exec($sql); $sqi = $pdo->exec("insert into tp_user value(2,'li','man')"); //识别错误回滚更改 var_dump($pdo->rollBack());
输出:bool(true)
Recommended: 《2021 PHP interview questions summary ( Collection)》《php video tutorial》
The above is the detailed content of Detailed explanation of rollBack() method in PHP. For more information, please follow other related articles on the PHP Chinese website!