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


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