mysqli_rollback()函數(shù)回退當(dāng)前事務(wù)
MySQL數(shù)據(jù)庫(kù)具有自動(dòng)提交事務(wù)的功能,如果您將其打開,則會(huì)自動(dòng)保存在數(shù)據(jù)庫(kù)中所做的更改;如果將其關(guān)閉,則需要使用mysqli_commit()函數(shù)顯式保存更改。
mysqli_rollback()函數(shù)將當(dāng)前事務(wù)滾動(dòng)到最后一個(gè)保存點(diǎn)(或指定的保存點(diǎn))。
mysqli_rollback($con, [$flags, $name]);
序號(hào) | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是一個(gè)表示與MySQL Server的連接的對(duì)象。 |
2 | flags(可選) 可以是下列常量之一:
|
3 | name(可選) 這是一個(gè)名稱值,當(dāng)給定時(shí),將作為 ROLLBACK /* name */ 執(zhí)行。 |
mysqli_rollback()函數(shù)返回一個(gè)布爾值,如果操作成功,則為true,否則為false。
此函數(shù)最初是在PHP版本5中引入的,并且在所有更高版本中均可用。
以下示例演示了mysqli_rollback()函數(shù)的用法(面向過程風(fēng)格)-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //將自動(dòng)提交設(shè)置為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("行數(shù)(提交時(shí)): ".mysqli_affected_rows($con)."\n"); //保存更改 mysqli_commit($con); //截?cái)啾? mysqli_query($con, "DELETE FROM my_team where id in(3,4)"); $res = mysqli_query($con, "SELECT * FROM my_team"); print("行數(shù)(回滾之前): ".mysqli_affected_rows($con)."\n"); //回退 mysqli_rollback($con); //表中的內(nèi)容 $res = mysqli_query($con, "SELECT * FROM my_team"); print("行數(shù)(回滾后): ".mysqli_affected_rows($con)); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
行數(shù)(提交時(shí)): 4 行數(shù)(回滾之前): 2 行數(shù)(回滾后): 4
面向?qū)ο箫L(fēng)格的此方法的語(yǔ)法為$con->rollback()。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
//建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); //將自動(dòng)提交設(shè)置為false $con->autocommit(FALSE); //在players表中插入一個(gè)記錄 $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')"); //保存結(jié)果 $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); //關(guān)閉連接 $res = $con -> close(); ?>
輸出結(jié)果
mysqli_result Object ( [current_field] => 0 [field_count] => 3 [lengths] => [num_rows] => 2 [type] => 0 )
關(guān)閉自動(dòng)提交,做一些查詢,提交查詢,然后回滾當(dāng)前事務(wù):
<?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); ?>
執(zhí)行以上程序后,如果驗(yàn)證表test的內(nèi)容,則可以看到插入的記錄為-
mysql> select * from test; +---------+------+ | Name | Age | +---------+------+ | Sharukh | 25 | +---------+------+ 1 row in set (0.00 sec)