编辑这个页面须要登录或更高权限!
mysqli_autocommit() 函数打开或关闭本次数据库连接的自动命令提交事务模式
MySQL数据库具有一项名为自动提交的特性,如果您将其打开,则会自动保存在数据库中所做的更改;如果将其关闭,则需要显式保存更改。mysqli_autocommit() 打开或关闭本次数据库连接的自动命令提交事务模式。如需要确认当前连接的自动事务提交状态,可执行这个SQL请求SELECT @@autocommit 。
此函数接受布尔值作为参数。如果将true传递给此函数,则将启用自动提交事务模式;如果传递false,则将关闭自动提交事务模式。
mysqli_autocommit($con, $mode);
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
2 | mode(必需) 这是一个布尔值,表示是否应打开自动提交事务模式。 |
PHP mysqli_autocommit()函数返回一个布尔值,成功时为true,失败时为false。
这个方法不会在不支持事务处理的表单查询中生效,如MyISAM或 ISAM。
此函数最初是在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 //建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //将自动提交设置为false mysqli_autocommit($con, False); //将记录插入到my_team表中 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')"); //验证表的内容 $result = mysqli_query($con, "SELECT * FROM my_team"); print_r($result); //关闭连接 mysqli_close($con); ?>
输出结果
mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 4 [type] => 0 )
由于在上一示例中我们已关闭了自动提交选项,因此记录添加将不会保存在数据库中,并且,如果您在MySQL中验证表的内容,则该表将为空,如下所示;
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()。以下是面向对象风格中此函数的示例;
//建立连接 $con = new mysqli("localhost", "root", "password", "mydb"); //将自动提交设置为true $con->autocommit(FALSE); //将记录插入到my_team表中 $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')"); //验证表的内容 $result = $con->query( "SELECT * FROM my_team"); print_r($result); //保存结果 $con->commit(); //关闭连接 $con -> close(); ?>
输出结果
mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 4 [type] => 0 )
mysqli_autocommit()函数在调用时也可用作commit(),它将等待查询的结果保存到数据库中:
//建立连接 $con = mysqli_connect("localhost", "root", "password", "mydb"); //创建表 mysqli_query($con, "Create table players (First_Name VARCHAR(255), Last_Name VARCHAR(255), Country VARCHAR(255))"); //将自动提交设置为false mysqli_autocommit($con, False); //将记录插入到my_team表中 mysqli_query($con, "insert into players values('Shikhar', 'Dhawan', 'India')"); mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')"); mysqli_autocommit($con, TRUE); //关闭连接 mysqli_close($con); ?>
执行程序后,如果您验证 players 表的内容,则可以观察添加的记录,如下所示-
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); ?>
执行以上程序后,如果验证表test的内容,则可以看到插入的记录为-
mysql> select * from test; +---------+------+ | Name | Age | +---------+------+ | Sharukh | 25 | | Kalyan | 30 | +---------+------+ 2 rows in set (0.00 sec)