定義和用法
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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。