Home  >  Article  >  Backend Development  >  Detailed explanation of rollBack() method in PHP

Detailed explanation of rollBack() method in PHP

autoload
autoloadOriginal
2021-04-27 10:06:153166browse

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

First let's take a look at the syntax of the rollBack() method:

PDO::rollBack    (   )

  • Roll back the current transaction initiated by

    PDO::beginTransaction() affairs. If no transaction is active, a PDOException will be thrown.

  • Return value: Return

    true on success, or false on failure.

Code example:


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,&#39;li&#39;,&#39;man&#39;)");

//识别错误回滚更改
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!

Statement:
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