mysqli_rollback()函数回退当前事务
MySQL数据库具有自动提交事务的功能,如果您将其打开,则会自动保存在数据库中所做的更改;如果将其关闭,则需要使用mysqli_commit()函数显式保存更改。
mysqli_rollback()函数将当前事务滚动到最后一个保存点(或指定的保存点)。
mysqli_rollback($con, [$flags, $name]);
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
2 | flags(可选) 可以是下列常量之一:
|
3 | name(可选) 这是一个名称值,当给定时,将作为 ROLLBACK /* name */ 执行。 |
mysqli_rollback()函数返回一个布尔值,如果操作成功,则为true,否则为false。
此函数最初是在PHP版本5中引入的,并且在所有更高版本中均可用。
以下示例演示了mysqli_rollback()函数的用法(面向过程风格)-
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //将自动提交设置为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))"); //将记录插入到my_team表中 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("行数(提交时): ".mysqli_affected_rows($con)."\n"); //保存更改 mysqli_commit($con); //截断表 mysqli_query($con, "DELETE FROM my_team where id in(3,4)"); $res = mysqli_query($con, "SELECT * FROM my_team"); print("行数(回滚之前): ".mysqli_affected_rows($con)."\n"); //回退 mysqli_rollback($con); //表中的内容 $res = mysqli_query($con, "SELECT * FROM my_team"); print("行数(回滚后): ".mysqli_affected_rows($con)); //关闭连接 mysqli_close($con); ?>
输出结果
行数(提交时): 4 行数(回滚之前): 2 行数(回滚后): 4
面向对象风格的此方法的语法为$con->rollback()。以下是面向对象风格中此函数的示例;
//建立连接 $con = new mysqli("localhost", "root", "password", "mydb"); //将自动提交设置为false $con->autocommit(FALSE); //在players表中插入一个记录 $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')"); //保存结果 $con->commit(); $con->query( "insert into players values('Kumara', 'Sangakkara', 'Srilanka')"); $con->query( "insert into players values('Virat', 'Kohli', 'India')"); //回退 $con->rollback(); $res = $con->query("SELECT * FROM players"); print_r($res); //关闭连接 $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 "连接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)