mysqli_autocommit() 函數(shù)打開或關(guān)閉本次數(shù)據(jù)庫(kù)連接的自動(dòng)命令提交事務(wù)模式
MySQL數(shù)據(jù)庫(kù)具有一項(xiàng)名為自動(dòng)提交的特性,如果您將其打開,則會(huì)自動(dòng)保存在數(shù)據(jù)庫(kù)中所做的更改;如果將其關(guān)閉,則需要顯式保存更改。mysqli_autocommit() 打開或關(guān)閉本次數(shù)據(jù)庫(kù)連接的自動(dòng)命令提交事務(wù)模式。如需要確認(rèn)當(dāng)前連接的自動(dòng)事務(wù)提交狀態(tài),可執(zhí)行這個(gè)SQL請(qǐng)求SELECT @@autocommit 。
此函數(shù)接受布爾值作為參數(shù)。如果將true傳遞給此函數(shù),則將啟用自動(dòng)提交事務(wù)模式;如果傳遞false,則將關(guān)閉自動(dòng)提交事務(wù)模式。
mysqli_autocommit($con, $mode);
序號(hào) | 參數(shù)及說(shuō)明 |
---|---|
1 | con(必需) 這是一個(gè)表示與MySQL Server的連接的對(duì)象。 |
2 | mode(必需) 這是一個(gè)布爾值,表示是否應(yīng)打開自動(dòng)提交事務(wù)模式。 |
PHP mysqli_autocommit()函數(shù)返回一個(gè)布爾值,成功時(shí)為true,失敗時(shí)為false。
這個(gè)方法不會(huì)在不支持事務(wù)處理的表單查詢中生效,如MyISAM或 ISAM。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
假設(shè)我們?cè)跀?shù)據(jù)庫(kù)mydb中創(chuàng)建了一個(gè)名為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_autocommit()函數(shù)的用法(以面向過(guò)程風(fēng)格)-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //將自動(dòng)提交設(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')"); //驗(yàn)證表的內(nèi)容 $result = mysqli_query($con, "SELECT * FROM my_team"); print_r($result); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 4 [type] => 0 )
由于在上一示例中我們已關(guān)閉了自動(dòng)提交選項(xiàng),因此記錄添加將不會(huì)保存在數(shù)據(jù)庫(kù)中,并且,如果您在MySQL中驗(yàn)證表的內(nèi)容,則該表將為空,如下所示;
mysql> select * from my_team; Empty set (0.00 sec)
要保存數(shù)據(jù)庫(kù)中的更改,需要在程序結(jié)束時(shí)使用mysqli_commit()函數(shù)將更改提交為
mysqli_commit($con);
如果您驗(yàn)證表 my_team 的內(nèi)容,則可以看到插入的記錄,如下所示-
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ū)ο箫L(fēng)格方式,此方法的語(yǔ)法為 $con-> autocommit()。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
//建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); //將自動(dòng)提交設(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')"); //驗(yàn)證表的內(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 )
mysqli_autocommit()函數(shù)在調(diào)用時(shí)也可用作commit(),它將等待查詢的結(jié)果保存到數(shù)據(jù)庫(kù)中:
//建立連接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //創(chuàng)建表 mysqli_query($con, "Create table players (First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))"); //將自動(dòng)提交設(shè)置為false mysqli_autocommit($con, False); //將記錄插入到my_team表中 mysqli_query($con, "insert into players values('Shikhar', 'Dhawan', 'India')"); mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')"); mysqli_autocommit($con, TRUE); //關(guān)閉連接 mysqli_close($con); ?>
執(zhí)行程序后,如果您驗(yàn)證 players 表的內(nèi)容,則可以觀察添加的記錄,如下所示-
mysql> select * from players; +------------+-----------+-------------+ | First_Name | Last_Name | Country | +------------+-----------+-------------+ | Shikhar | Dhawan | India | | Jonathan | Trott | SouthAfrica | +------------+-----------+-------------+ 2 rows in set (0.00 sec)
<?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_query($connection, "INSERT INTO test VALUES ('Kalyan', 30)"); mysqli_commit($connection); mysqli_close($connection); ?>
執(zhí)行以上程序后,如果驗(yàn)證表test的內(nèi)容,則可以看到插入的記錄為-
mysql> select * from test; +---------+------+ | Name | Age | +---------+------+ | Sharukh | 25 | | Kalyan | 30 | +---------+------+ 2 rows in set (0.00 sec)