定义和用法
MySQL 数据库有一个名为 auto-commit 的函数,如果打开它,数据库中所做的更改会自动保存,如果关闭,则需要使用 mysqli_commit() 函数显式保存更改。
这个mysqli_rollback()函数将当前事务滚动到最后一个保存点(或指定的保存点)。
用法
mysqli_rollback($con, [$flags, $name]);
参数
Sr.No | 参数及说明 |
---|---|
1 |
con(Mandatory) 这是一个表示与 MySQL 服务器的连接的对象。 |
2 |
flags(Optional) 一个常数,可以是以下之一:
|
3 |
name(Optional) 这是一个名称值,当给出时,作为 ROLLBACK/*name*/执行。 |
返回值
PHP mysqli_rollback() 函数返回一个布尔值,如果操作成功则为真,否则为假。
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()。以下是面向对象模式 $minus 中此函数的示例;
//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);
?>
执行上述程序后,如果您验证表测试的内容,您可以看到插入的记录为 -
mysql> select * from test; +---------+------+ | Name | Age | +---------+------+ | Sharukh | 25 | +---------+------+ 1 row in set (0.00 sec)
相关用法
- PHP mysqli_real_escape_string()用法及代码示例
- PHP mysqli_real_connect()用法及代码示例
- PHP mysqli_refresh()用法及代码示例
- PHP mysqli_real_query()用法及代码示例
- PHP mysqli_get_server_info()用法及代码示例
- PHP mysqli_data_seek()用法及代码示例
- PHP mysqli_insert_id()用法及代码示例
- PHP mysqli_fetch_assoc()用法及代码示例
- PHP mysqli_connect_error()用法及代码示例
- PHP mysqli_fetch_all()用法及代码示例
- PHP mysqli_next_result()用法及代码示例
- PHP mysqli_stmt_affected_rows()用法及代码示例
- PHP mysqli_begin_transaction()用法及代码示例
- PHP mysqli_fetch_lengths()用法及代码示例
- PHP mysqli_character_set_name()用法及代码示例
- PHP mysqli_free_result()用法及代码示例
- PHP mysqli_fetch_row()用法及代码示例
- PHP mysqli_debug()用法及代码示例
- PHP mysqli_commit()用法及代码示例
- PHP mysqli_fetch_field()用法及代码示例
注:本文由纯净天空筛选整理自 PHP mysqli_rollback() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。