目录

PHP mysqli rollback() 函数

❮ PHP mysqli 参考

示例 - 面向对象风格

关闭自动提交,进行一些查询,提交查询,然后回滚当前事务:

<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// Turn autocommit off
$mysqli -> autocommit(FALSE);

// Insert some values
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");

// Commit transaction
if (!$mysqli -> commit()) {
  echo "Commit transaction failed";
  exit();
}

// Rollback transaction
$mysqli -> rollback();

$mysqli -> close();
?>

查看底部的程序样式示例。


定义和用法

rollback() / mysqli_rollback() 函数回滚指定数据库连接的当前事务。

提示:另请参阅犯罪()函数,提交当前事务,以及自动提交()函数,它打开或关闭自动提交数据库修改。


语法

面向对象风格:

$mysqli -> rollback( flags, name)

程序风格:

mysqli_rollback( connection, flags, name)

参数值

Parameter Description
connection Required. Specifies the MySQL connection to use
flags Optional. A constant:
  • MYSQLI_TRANS_COR_AND_CHAIN - Appends "AND CHAIN"
  • MYSQLI_TRANS_COR_AND_NO_CHAIN - Appends "AND NO CHAIN"
  • MYSQLI_TRANS_COR_RELEASE - Appends "RELEASE"
  • MYSQLI_TRANS_COR_NO_RELEASE - Appends "NO RELEASE"
name Optional. ROLLBACK/*name*/ is executed if this parameter is specified

技术细节

返回值: 成功则为真。失败时为 FALSE
PHP 版本: 5+
PHP 变更日志: PHP 5.5:添加了旗帜姓名参数

示例 - 程序风格

关闭自动提交,进行一些查询,提交查询,然后回滚当前事务:

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit;
}

// Turn autocommit off
mysqli_autocommit($con,FALSE);

// Insert some values
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");

// Commit transaction
if (!$mysqli_commit($con)) {
  echo "Commit transaction failed";
  exit();
}

// Rollback transaction
mysqli_rollback($con);

// Close connection
mysqli_close($con);
?>


❮ PHP mysqli 参考