PHP mysqli_autocommit() 函式



定義和用法

MySQL 資料庫有一個名為自動提交的功能,如果您將其開啟,則對資料庫所做的更改將自動儲存,並且,如果您將其關閉,則需要顯式儲存更改。mysqli_autocommit() 用於開啟/關閉自動提交功能。

此函式接受布林值作為引數。如果您將true傳遞給此函式,則自動提交功能將被開啟,如果您傳遞false,則此函式將關閉自動提交功能。

語法

mysqli_autocommit($con, $mode);

引數

序號 引數和描述
1

con(必填)

這是一個表示與 MySQL 伺服器連線的物件。

2

mode(必填)

這是一個布林值,表示是否應開啟自動提交模式。

返回值

PHP mysqli_autocommit() 函式返回一個布林值,成功時為true,失敗時為false

PHP 版本

此函式首次引入於 PHP 5 版本,並在所有後續版本中均有效。

示例

假設我們在資料庫 mydb 中建立了一個名為 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() 函式(以過程化風格)的使用:

<?php
   //Creating a connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");

   //Setting auto commit to false
   mysqli_autocommit($con, False);

   //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')");

   //Verifying the contents of the table
   $result = mysqli_query($con, "SELECT * FROM my_team");
   print_r($result);

   //Closing the connection
   mysqli_close($con);
?>

這將產生以下結果:

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 5
    [lengths] =>
    [num_rows] => 4
    [type] => 0
)

由於我們在前面的示例中關閉了自動提交選項,因此新增的記錄將不會儲存在資料庫中,並且,如果您在 MySQL 中驗證表的內容,它將為空,如下所示 $minus;

mysql> select * from my_team;
Empty set (0.00 sec)

要儲存資料庫中的更改,您需要在程式結束時使用 mysqli_commit() 函式提交更改,如下所示

mysqli_commit($con);

如果您驗證 my_team 表的內容,那麼您可以看到插入的記錄,如下所示:

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)

示例

此方法在面向物件風格中的語法為$con->autocommit()。以下是此函式在面向物件模式下的示例 $minus;

//Creating a connection
$con = new mysqli("localhost", "root", "password", "mydb");

//Setting auto commit to true
$con->autocommit(FALSE);

//Inserting a records into the my_team table
$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')");

//Verifying the contents of the table
$result = $con->query( "SELECT * FROM my_team");
print_r($result);

//Saving the results
$con->commit();

//Closing the connection
$con -> close();
?>

這將產生以下結果:

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 5
    [lengths] =>
    [num_rows] => 4
    [type] => 0
)

示例

mysqli_autocommit() 函式在呼叫時也充當 commit(),它將等待查詢的結果儲存到資料庫中:

//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "mydb");

//creating a table
mysqli_query($con, "Create table players (First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))");

//Setting auto commit to false
mysqli_autocommit($con, False);

//Inserting a records into the my_team table
mysqli_query($con, "insert into players values('Shikhar', 'Dhawan', 'India')");
mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

mysqli_autocommit($con, TRUE);

//Closing the connection
mysqli_close($con);
?>

執行程式後,如果您驗證players表的內容,您可以觀察到新增的記錄,如下所示:

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);
?>

執行上述程式後,如果您驗證test表的內容,您可以看到插入的記錄,如下所示:

mysql> select * from test;
+---------+------+
| Name    | Age  |
+---------+------+
| Sharukh |   25 |
| Kalyan  |   30 |
+---------+------+
2 rows in set (0.00 sec)
php_function_reference.htm
廣告