PHP mysqli_rollback() 函数



定义和用法

MySQL 数据库有一个名为自动提交的功能,如果启用它,对数据库所做的更改会自动保存;如果禁用它,则需要使用 mysqli_commit() 函数显式保存更改。

mysqli_rollback() 函数将当前事务回滚到最后一个保存点(或指定的保存点)。

语法

mysqli_rollback($con, [$flags, $name]);

参数

序号 参数和描述
1

con(必填)

这是一个表示与 MySQL 服务器连接的对象。

2

flags(可选)

一个常量,可以是以下之一:

  • MYSQLI_TRANS_COR_AND_CHAIN

  • MYSQLI_TRANS_COR_AND_NO_CHAIN

  • MYSQLI_TRANS_COR_RELEASE

  • MYSQLI_TRANS_COR_NO_RELEASE

3

name(可选)

这是一个名称值,如果给出,则执行为 ROLLBACK/*name*/

返回值

PHP mysqli_rollback() 函数返回一个布尔值,如果操作成功则为 true,否则为 false

PHP 版本

此函数首次出现在 PHP 5 版本中,并在所有后续版本中均有效。

示例

以下示例演示了 mysqli_rollback() 函数的用法(过程式风格):-

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Setting auto commit to false
   mysqli_autocommit($con, False);
   mysqli_query($con, "CREATE TABLE IF NOT EXISTS my_team(ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255))");
   //Inserting a records into the my_team table
   mysqli_query($con, "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')");
   mysqli_query($con, "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')");
   mysqli_query($con, "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   mysqli_query($con, "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')");

   $res = mysqli_query($con, "SELECT * FROM my_team");
   print("No.of rows (at the time of commit): ".mysqli_affected_rows($con)."\n");

   //Saving the changes
   mysqli_commit($con);

   //Truncating the table
   mysqli_query($con, "DELETE FROM my_team where id in(3,4)"); 
   $res = mysqli_query($con, "SELECT * FROM my_team");
   print("No.of rows (before roll back): ".mysqli_affected_rows($con)."\n");

   //Roll back
   mysqli_rollback($con);

   //Contents of the table
   $res = mysqli_query($con, "SELECT * FROM my_team");
   print("No.of rows (after roll back): ".mysqli_affected_rows($con));

   //Closing the connection
   mysqli_close($con);
?>

这将产生以下结果:-

No.of rows (at the time of commit): 4
No.of rows (before roll back): 2
No.of rows (after roll back): 4

示例

此方法在面向对象风格中的语法为 $con->rollback()。以下是此函数在面向对象模式下的示例:

//Creating a connection
$con = new mysqli("localhost", "root", "password", "mydb");

//Setting auto commit to false
$con->autocommit(FALSE);

//Inserting a records into the players table
$con->query("CREATE TABLE IF NOT EXISTS players(First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))");
$con->query("insert into players values('Shikhar', 'Dhawan', 'India')");
$con->query("insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

//Saving the results
$con->commit();

$con->query( "insert into players values('Kumara', 'Sangakkara', 'Srilanka')");
$con->query( "insert into players values('Virat', 'Kohli', 'India')");

//Roll back
$con->rollback();

$res = $con->query("SELECT * FROM players");
print_r($res);


//Closing the connection
$res = $con -> close();
?>

这将产生以下结果:-

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 3
    [lengths] =>
    [num_rows] => 2
    [type] => 0
)

示例

<?php
   $connection = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }
   mysqli_autocommit($connection,FALSE); 
   
   mysqli_query($connection, "create table test(Name VARCHAR(255), Age INT)");   
   mysqli_query($connection, "INSERT INTO test VALUES ('Sharukh', 25)");

   mysqli_commit($connection);
   
   mysqli_query($connection, "INSERT INTO test VALUES ('Kalyan', 30)");
   
   mysqli_rollback($connection);
   mysqli_close($connection);
?>

执行上述程序后,如果验证 test 表的内容,您可以看到插入的记录如下:-

mysql> select * from test;
+---------+------+
| Name    | Age  |
+---------+------+
| Sharukh |   25 |
+---------+------+
1 row in set (0.00 sec)
php_function_reference.htm
广告