mysqli_commit()函數(shù)提交一個事務(wù)
MySQL數(shù)據(jù)庫具有一項稱為自動提交事務(wù)(如果您將其打開),則數(shù)據(jù)庫中所做的更改將自動保存;如果將其關(guān)閉,則需要顯式保存更改。
mysqli_commit()函數(shù)保存當(dāng)前的事務(wù)。
mysqli_commit($con, [$flags, $name]);
序號 | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是一個表示與MySQL Server的連接的對象。 |
2 | flags(可選) 可以是下列常量之一:
|
3 | name(可選) 這是一個名稱值,當(dāng)給定時,將以COMMIT /* name */的形式執(zhí)行。 |
mysqli_commit()函數(shù)返回一個布爾值,如果提交操作成功,則為true,否則為false。
此函數(shù)最初是在PHP版本5中引入的,并且在所有更高版本中均可用。
假設(shè)我們在數(shù)據(jù)庫mydb中創(chuàng)建了一個名為my_team的表,如下所示-
CREATE TABLE my_team( ID INT PRIMARY KEY AUTO_INCREMENT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Place_Of_Birth VARCHAR(255), Country VARCHAR(255) );
以下示例演示了mysqli_commit()函數(shù)的用法(面向過程風(fēng)格)-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //將自動提交設(shè)置為false mysqli_autocommit($con, False); //將記錄插入到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')"); //驗證表的內(nèi)容 $result = mysqli_query($con, "SELECT * FROM my_team"); print_r($result); mysqli_commit($con); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 4 [type] => 0 )
如果您驗證表my_team的內(nèi)容,則可以觀察保存的數(shù)據(jù),如下所示-
mysql> select * from my_team; +----+------------+------------+----------------+-------------+ | ID | First_Name | Last_Name | Place_Of_Birth | Country | +----+------------+------------+----------------+-------------+ | 1 | Shikhar | Dhawan | Delhi | India | | 2 | Jonathan | Trott | CapeTown | SouthAfrica | | 3 | Kumara | Sangakkara | Matale | Srilanka | | 4 | Virat | Kohli | Delhi | India | +----+------------+------------+----------------+-------------+ 4 rows in set (0.00 sec)
面向?qū)ο髽邮降姆椒?,語法為$con->commit()。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
//建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); //將自動提交設(shè)置為true $con->autocommit(FALSE); //將記錄插入到my_team表中 $con->query( "insert into my_team values(1, 'Shikhar', 'Dhawan', 'Delhi', 'India')"); $con->query( "insert into my_team values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')"); $con->query( "insert into my_team values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')"); $con->query( "insert into my_team values(4, 'Virat', 'Kohli', 'Delhi', 'India')"); //驗證表的內(nèi)容 $result = $con->query( "SELECT * FROM my_team"); print_r($result); //保存結(jié)果 $con->commit(); //關(guān)閉連接 $con -> close(); ?>
輸出結(jié)果
mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 4 [type] => 0 )
讓我們再看另一個實例。 在這里,我們創(chuàng)建了一個表,關(guān)閉了自動提交選項,插入了一條記錄并保存了更改。 保存之后,我們插入了另一條記錄:
//建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "Create table players (First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))"); //將自動提交設(shè)置為false mysqli_autocommit($con, False); //將記錄插入到my_team表中 mysqli_query($con, "insert into players values('Shikhar', 'Dhawan', 'India')"); mysqli_commit($con); mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')"); //關(guān)閉連接 mysqli_close($con); ?>
由于我們尚未保存上一個插入查詢,因此在執(zhí)行上述程序后,如果您驗證 players 表的內(nèi)容,則只能看到一條記錄,如下所示-
mysql> select * from players; +------------+-----------+---------+ | First_Name | Last_Name | Country | +------------+-----------+---------+ | Shikhar | Dhawan | India | +------------+-----------+---------+ 1 row in set (0.00 sec)
<?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_query($connection, "INSERT INTO test VALUES ('Kalyan', 30)"); mysqli_commit($connection); mysqli_close($connection); ?>
執(zhí)行以上程序后,如果驗證表test的內(nèi)容,則可以看到插入的記錄為-
mysql> select * from test; +---------+------+ | Name | Age | +---------+------+ | Sharukh | 25 | | Kalyan | 30 | +---------+------+ 2 rows in set (0.00 sec)