编辑这个页面须要登录或更高权限!
mysqli_commit()函数提交一个事务
MySQL数据库具有一项称为自动提交事务(如果您将其打开),则数据库中所做的更改将自动保存;如果将其关闭,则需要显式保存更改。
mysqli_commit()函数保存当前的事务。
mysqli_commit($con, [$flags, $name]);
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
2 | flags(可选) 可以是下列常量之一:
|
3 | name(可选) 这是一个名称值,当给定时,将以COMMIT /* name */的形式执行。 |
mysqli_commit()函数返回一个布尔值,如果提交操作成功,则为true,否则为false。
此函数最初是在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 //建立连接 $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_commit($con); //关闭连接 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()。以下是面向对象风格中此函数的示例;
//建立连接 $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 )
让我们再看另一个实例。 在这里,我们创建了一个表,关闭了自动提交选项,插入了一条记录并保存了更改。 保存之后,我们插入了另一条记录:
//建立连接 $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_commit($con); mysqli_query($con, "insert into players values('Jonathan', 'Trott', 'SouthAfrica')"); //关闭连接 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 "连接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)