MySQLi - Rollback

语法

bool mysqli_rollback ( mysqli $link [, int $flags [, string $name ]] )

定义和用法

它从当前事务的保存点集中删除指定的保存点。

示例

试试下面的例子 −

<?php
   $servername = "localhost:3306";
   $username = "root";
   $password = "";
   $dbname = "TUTORIALS";
   $tmp = NULL;
   $conn = new mysqli($servername, $username, $password, $dbname);
   
   if (!$conn->real_connect($servername, $username, $password, $dbname)) {
      die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
   }
   echo 'Success... ' . mysqli_get_host_info($conn) . "
";
   
   mysqli_query($conn, "CREATE TABLE tutorials_test1 LIKE tutorials_auto");
   mysqli_query($conn, "ALTER TABLE tutorials_test1 Type = InnoDB");
   mysqli_query($conn, "INSERT INTO tutorials_test1 SELECT * FROM id LIMIT 50");
   mysqli_commit($conn);
   mysqli_query($conn, "DELETE FROM tutorials_test1");
   
   if ($result = mysqli_query($conn, "SELECT COUNT(*) FROM tutorials_test1")) {
      $row = mysqli_fetch_row($result);
      printf("%d rows in table tutorials_test1.
", $row[0]);
      mysqli_free_result($result);
   }
   mysqli_rollback($conn);

   if ($result = mysqli_query($conn, "SELECT COUNT(*) FROM tutorials_test1")) {
      $row = mysqli_fetch_row($result);
      printf("%d rows in table tutorials_test1 (after rollback).
", $row[0]);
      mysqli_free_result($result);
   }
   mysqli_query($conn, "DROP TABLE tutorials_test1");
   
   $conn->close();
?>

上述代码的示例输出应该是这样的 −

Success... localhost:3306 via TCP/IP 0 rows in table tutorials_test1. 
0 rows in table tutorials_test1 (after rollback).

mysqli_useful_functions.html