當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP mysqli_autocommit()用法及代碼示例


定義和用法

MySQL 數據庫有一個名為 auto-commit 的函數,如果打開它,數據庫中所做的更改會自動保存,如果關閉,則需要顯式保存更改。這mysqli_autocommit()用於打開/關閉 auto-commit 函數。

該函數接受一個布爾值作為參數。如果您向此函數傳遞 true,則 auto-commit 函數將被打開,如果您傳遞 false,則會關閉 auto-commit 函數。

用法

mysqli_autocommit($con, $mode);

參數

Sr.No 參數及說明
1

con(Mandatory)

這是一個表示與 MySQL 服務器的連接的對象。

2

mode(Mandatory)

這是一個布爾值,表示是否應打開 auto-commit 模式。

返回值

PHP mysqli_autocommit() 函數返回一個布爾值,成功時為真,失敗時為假。

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
)

由於我們在前麵的例子中關閉了 auto-commit 選項,添加的記錄將不會保存在數據庫中,如果您在 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);
?>

執行程序後,如果您驗證玩家表的內容,您可以觀察添加的記錄,如下所示 -

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

執行上述程序後,如果您驗證表測試的內容,您可以看到插入的記錄為 -

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

相關用法


注:本文由純淨天空篩選整理自 PHP mysqli_autocommit() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。