(PHP 5, PHP 7, PHP 8)
mysqli::autocommit -- mysqli_autocommit — 打開或關(guān)閉本次數(shù)據(jù)庫連接的自動命令提交事務(wù)模式
面向?qū)ο箫L(fēng)格
$mode
): bool過程化風(fēng)格
打開或關(guān)閉本次數(shù)據(jù)庫連接的自動命令提交事務(wù)模式。
如需要確認(rèn)當(dāng)前連接的自動事務(wù)提交狀態(tài),可執(zhí)行這個SQL請求SELECT @@autocommit
.
mysql
僅以過程化樣式:由mysqli_connect() 或 mysqli_init() 返回的 mysqli 對象。
mode
Whether to turn on auto-commit or not.
成功時返回 true
, 或者在失敗時返回 false
。
注意:
這個方法不會在不支持事務(wù)處理的表單查詢中生效,如MyISAM或 ISAM。
示例 #1 mysqli::autocommit() example
面向?qū)ο箫L(fēng)格
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* turn autocommit on */
$mysqli->autocommit(TRUE);
if ($result = $mysqli->query("SELECT @@autocommit")) {
$row = $result->fetch_row();
printf("Autocommit is %s\n", $row[0]);
$result->free();
}
/* close connection */
$mysqli->close();
?>
過程化風(fēng)格
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
if (!$link) {
printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
exit();
}
/* turn autocommit on */
mysqli_autocommit($link, TRUE);
if ($result = mysqli_query($link, "SELECT @@autocommit")) {
$row = mysqli_fetch_row($result);
printf("Autocommit is %s\n", $row[0]);
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
?>
以上例程會輸出:
Autocommit is 1