Hàm autocommit() của PHP mysqli
Ví dụ - Phong cách hướng đối tượng
Tắt tự động xác nhận, thực hiện một số truy vấn, sau đó xác nhận các truy vấn:
<?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();
?>
Nhìn vào ví dụ về phong cách thủ tục ở phía dưới.
Định nghĩa và cách sử dụng
Hàm autocommit() / mysqli_autocommit() bật hoặc tắt các sửa đổi cơ sở dữ liệu tự động cam kết.
Mẹo: Ngoài ra, hãy xem hàm commit() , cam kết giao dịch hiện tại cho kết nối cơ sở dữ liệu đã chỉ định và hàm rollback() , sẽ khôi phục giao dịch hiện tại.
Cú pháp
Phong cách hướng đối tượng:
$mysqli -> autocommit( mode )
Phong cách thủ tục:
mysqli_autocommit( connection, mode )
Giá trị tham số
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
mode | Required. FALSE turns auto-commit off. TRUE turns auto-commit on (and commits any waiting queries) |
Chi tiết kỹ thuật
Giá trị trả về: | ĐÚNG khi thành công. FALSE khi thất bại |
---|---|
Phiên bản PHP: | 5+ |
Ví dụ - Kiểu thủ tục
Tắt tự động xác nhận, thực hiện một số truy vấn, sau đó xác nhận các truy vấn:
<?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);
?>
❮ Tham khảo PHP MySQLi