当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP mysqli_commit()用法及代码示例



定义和用法

MySQL 数据库有一个名为 auto-commit 的函数,如果打开它,数据库中所做的更改会自动保存,如果关闭,则需要显式保存更改。

这个mysqli_commit()函数保存当前交易。

用法

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

参数

Sr.No 参数及说明
1

con(Mandatory)

这是一个表示与 MySQL 服务器的连接的对象。

2

flags(Optional)

一个常数,可以是以下之一:

  • MYSQLI_TRANS_COR_AND_CHAIN

  • MYSQLI_TRANS_COR_AND_NO_CHAIN

  • MYSQLI_TRANS_COR_RELEASE

  • MYSQLI_TRANS_COR_NO_RELEASE

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_commit() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。