mysqli::begin_transaction
mysqli_begin_transaction
(PHP 5 >= 5.5.0, PHP 7)
mysqli::begin_transaction -- mysqli_begin_transaction — Starts a transaction
说明
面向对象风格 (method):
$flags
= 0 [,
string $name
]] ) :
bool
过程化风格:
Begins a transaction. Requires the InnoDB engine (it is enabled by default). For additional details about how MySQL transactions work, see » http://dev.mysql.com/doc/mysql/en/commit.html.
参数
-
link
-
仅以过程化样式:由mysqli_connect() 或 mysqli_init() 返回的链接标识。
-
flags
-
Valid flags are:
-
MYSQLI_TRANS_START_READ_ONLY
: Start the transaction as "START TRANSACTION READ ONLY". Requires MySQL 5.6 and above. -
MYSQLI_TRANS_START_READ_WRITE
: Start the transaction as "START TRANSACTION READ WRITE". Requires MySQL 5.6 and above. -
MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT
: Start the transaction as "START TRANSACTION WITH CONSISTENT SNAPSHOT".
-
-
name
-
Savepoint name for the transaction.
返回值
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
范例
Example #1 $mysqli->begin_transaction() example
面向对象风格
<?php
$mysqli = new mysqli("127.0.0.1", "my_user", "my_password", "sakila");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$mysqli->begin_transaction(MYSQLI_TRANS_START_READ_ONLY);
$mysqli->query("SELECT first_name, last_name FROM actor");
$mysqli->commit();
$mysqli->close();
?>
过程化风格
<?php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "sakila");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_ONLY);
mysqli_query($link, "SELECT first_name, last_name FROM actor LIMIT 1");
mysqli_commit($link);
mysqli_close($link);
?>
参见
- mysqli_autocommit() - 打开或关闭本次数据库连接的自动命令提交事务模式
- mysqli_commit() - 提交一个事务
- mysqli_rollback() - 回退当前事务