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
广告