PHP mysqli autocommit() 函数
示例 - 面向对象的样式
关闭自动提交,做一些查询,然后提交查询:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Turn autocommit off
$mysqli -> autocommit(FALSE);
// Insert some values
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
// Commit transaction
if (!$mysqli -> commit()) {
echo "Commit transaction failed";
exit();
}
$mysqli -> close();
?>
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Turn autocommit off
$mysqli -> autocommit(FALSE);
// Insert some values
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
$mysqli -> query("INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
// Commit transaction
if (!$mysqli -> commit()) {
echo "Commit transaction failed";
exit();
}
$mysqli -> close();
?>
查看底部的程序样式示例。
定义和用法
autocommit() / mysqli_autocommit() 函数开启或关闭自动提交数据库修改。
提示: 请查看 mysqli_commit() 函数,用于提交指定数据库连接的当前事务。请查看 mysqli_rollback() 函数,用于回滚当前事务。
语法
面向对象的风格:
$mysqli -> autocommit(mode)
程序风格:
mysqli_autocommit(connection, mode)
参数值
参数 | 描述 |
---|---|
connection | 必需。规定要使用的 MySQL 连接。 |
mode | 必需。如果设置为 FALSE,则表示关闭 auto-commit。如果设置为 TRUE,则表示开启 auto-commit(提交任何等待查询)。 |
技术细节
返回值: | 若成功则返回 TRUE,若失败则返回 FALSE。 |
---|---|
PHP 版本: | 5+ |
示例 - 程序样式
关闭自动提交,做一些查询,然后提交查询:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
// Turn autocommit off
mysqli_autocommit($con,FALSE);
// Insert some values
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
// Commit transaction
if (!$mysqli_commit($con)) {
echo "Commit transaction failed";
exit();
}
// Close connection
mysqli_close($con);
?>
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit;
}
// Turn autocommit off
mysqli_autocommit($con,FALSE);
// Insert some values
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Peter','Griffin',35)");
mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");
// Commit transaction
if (!$mysqli_commit($con)) {
echo "Commit transaction failed";
exit();
}
// Close connection
mysqli_close($con);
?>