定義和用法
MySQL 數據庫有一個名為 auto-commit 的函數,如果打開它,數據庫中所做的更改會自動保存,如果關閉,則需要顯式保存更改。
這個mysqli_commit()函數保存當前交易。
用法
mysqli_commit($con, [$flags, $name]);
參數
Sr.No | 參數及說明 |
---|---|
1 |
con(Mandatory) 這是一個表示與 MySQL 服務器的連接的對象。 |
2 |
flags(Optional) 一個常數,可以是以下之一:
|
3 |
name(Optional) 這是一個名稱值,當給出時,作為 COMMIT/*name*/執行。 |
返回值
PHP mysqli_commit() 函數返回一個布爾值,如果提交操作成功,則為真,否則為假。
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()。以下是麵向對象模式 $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 )
示例
讓我們考慮另一個例子。在這裏,我們創建了一個表,關閉了 auto-commit 選項並插入了一條記錄並保存了更改。保存後,我們插入了另一條記錄 -
//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);
?>
由於我們還沒有保存最後一個插入查詢,執行上述程序後,如果您驗證玩家表的內容,您隻能看到一條記錄,如下所示 -
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);
?>
執行上述程序後,如果您驗證表測試的內容,您可以看到插入的記錄為 -
mysql> select * from test; +---------+------+ | Name | Age | +---------+------+ | Sharukh | 25 | | Kalyan | 30 | +---------+------+ 2 rows in set (0.00 sec)
相關用法
- PHP mysqli_connect_error()用法及代碼示例
- PHP mysqli_connect_errno()用法及代碼示例
- PHP mysqli_connect()用法及代碼示例
- PHP mysqli_character_set_name()用法及代碼示例
- PHP mysqli_change_user()用法及代碼示例
- PHP mysqli_close()用法及代碼示例
- PHP mysqli_get_server_info()用法及代碼示例
- PHP mysqli_data_seek()用法及代碼示例
- PHP mysqli_insert_id()用法及代碼示例
- PHP mysqli_fetch_assoc()用法及代碼示例
- PHP mysqli_fetch_all()用法及代碼示例
- PHP mysqli_next_result()用法及代碼示例
- PHP mysqli_stmt_affected_rows()用法及代碼示例
- PHP mysqli_begin_transaction()用法及代碼示例
- PHP mysqli_real_escape_string()用法及代碼示例
- PHP mysqli_fetch_lengths()用法及代碼示例
- PHP mysqli_free_result()用法及代碼示例
- PHP mysqli_fetch_row()用法及代碼示例
- PHP mysqli_debug()用法及代碼示例
- PHP mysqli_fetch_field()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP mysqli_commit() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。