Hàm cam kết mysqli() của PHP
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 commit() / mysqli_commit() cam kết giao dịch hiện tại cho kết nối cơ sở dữ liệu được chỉ định.
Mẹo: Ngoài ra, hãy xem hàm autocommit() , hàm này bật hoặc tắt các sửa đổi cơ sở dữ liệu tự động cam kết và hàm rollback() , hàm này sẽ khôi phục giao dịch hiện tại.
Cú pháp
Phong cách hướng đối tượng:
$mysqli -> commit( flags, name )
Phong cách thủ tục:
mysqli_commit( connection, flags, name )
Giá trị tham số
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
flags | Optional. A constant:
|
name | Optional. COMMIT/* name */ is executed if this parameter is specified |
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+ |
Nhật ký thay đổi PHP: | PHP 5.5: Đã thêm các tham số cờ và tên |
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