Hàm mysqli commit () trong PHP
Ví dụ - Kiểu hướng đối tượng
Tắt tính năng tự động cam kết, 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();
?>
Xem 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: Cũng xem xét 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 () , 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ả lại: | ĐÚNG về thành công. FALSE khi thất bại |
---|---|
Phiên bản PHP: | 5+ |
PHP Changelog: | PHP 5.5: Đã thêm các cờ và tham số tên |
Ví dụ - Phong cách thủ tục
Tắt tính năng tự động cam kết, 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