PHP mysqli_commit() 函式



定義和用法

MySQL 資料庫有一個名為自動提交的功能,如果啟用它,對資料庫所做的更改會自動儲存;如果停用它,則需要顯式儲存更改。

mysqli_commit() 函式儲存當前事務。

語法

mysqli_commit($con, [$flags, $name]);

引數

序號 引數及描述
1

con (必填)

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

2

flags (可選)

一個常量,可以是以下之一:

  • MYSQLI_TRANS_COR_AND_CHAIN

  • MYSQLI_TRANS_COR_AND_NO_CHAIN

  • MYSQLI_TRANS_COR_RELEASE

  • MYSQLI_TRANS_COR_NO_RELEASE

3

name (可選)

這是一個名稱值,當給出時,執行為 `COMMIT/*name*`。

返回值

PHP mysqli_commit() 函式返回一個布林值:如果提交操作成功,則為 `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_commit()` 函式(過程式風格)的用法:

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

   mysqli_commit($con);

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

這將產生以下結果:

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

如果驗證 `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->commit()`。以下是此函式在面向物件模式下的示例:

//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
)

示例

讓我們考慮另一個示例。在這裡,我們建立了一個表,關閉了自動提交選項,插入了一條記錄並儲存了更改。儲存後,我們又插入了另一條記錄:

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

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_commit($con);

mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')");

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

由於我們沒有儲存最後一條插入查詢,因此在執行上述程式後,如果驗證 `players` 表的內容,則只會看到一條記錄,如下所示:

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 "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
廣告