mysqli::rollback
mysqli::rollback
mysqli_rollback
(PHP 5, PHP 7)
mysqli :: rollback - mysqli_rollback - 回退当前事务
描述
面向对象的风格
bool mysqli::rollback ([ int $flags [, string $name ]] )
程序风格
bool mysqli_rollback ( mysqli $link [, int $flags [, string $name ]] )
回滚数据库的当前事务。
参数
`link`
仅过程样式:由mysqli_connect()或mysqli_init()返回的链接标识符
flags
MYSQLI_TRANS_COR_*
常量的位掩码。
name
如果提供,则执行ROLLBACK / * name * /
。
返回值
成功返回TRUE或失败时返回FALSE。
更新日志
版 | 描述 |
---|---|
5.5.0 | 添加标志和名称参数。 |
例子
示例#1 mysqli :: rollback()示例
面向对象的风格
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
/* disable autocommit */
$mysqli->autocommit(FALSE
$mysqli->query("CREATE TABLE myCity LIKE City"
$mysqli->query("ALTER TABLE myCity Type=InnoDB"
$mysqli->query("INSERT INTO myCity SELECT * FROM City LIMIT 50"
/* commit insert */
$mysqli->commit(
/* delete all rows */
$mysqli->query("DELETE FROM myCity"
if ($result = $mysqli->query("SELECT COUNT(*) FROM myCity")) {
$row = $result->fetch_row(
printf("%d rows in table myCity.\n", $row[0]
/* Free result */
$result->close(
}
/* Rollback */
$mysqli->rollback(
if ($result = $mysqli->query("SELECT COUNT(*) FROM myCity")) {
$row = $result->fetch_row(
printf("%d rows in table myCity (after rollback).\n", $row[0]
/* Free result */
$result->close(
}
/* Drop table myCity */
$mysqli->query("DROP TABLE myCity"
$mysqli->close(
?>
程序风格
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world"
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error()
exit(
}
/* disable autocommit */
mysqli_autocommit($link, FALSE
mysqli_query($link, "CREATE TABLE myCity LIKE City"
mysqli_query($link, "ALTER TABLE myCity Type=InnoDB"
mysqli_query($link, "INSERT INTO myCity SELECT * FROM City LIMIT 50"
/* commit insert */
mysqli_commit($link
/* delete all rows */
mysqli_query($link, "DELETE FROM myCity"
if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
$row = mysqli_fetch_row($result
printf("%d rows in table myCity.\n", $row[0]
/* Free result */
mysqli_free_result($result
}
/* Rollback */
mysqli_rollback($link
if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
$row = mysqli_fetch_row($result
printf("%d rows in table myCity (after rollback).\n", $row[0]
/* Free result */
mysqli_free_result($result
}
/* Drop table myCity */
mysqli_query($link, "DROP TABLE myCity"
mysqli_close($link
?>
上面的例子会输出:
0 rows in table myCity.
50 rows in table myCity (after rollback).
扩展内容
- mysqli_begin_transaction() - 启动一个事务
- mysqli_commit() - 提交当前事务
- mysqli_autocommit() - 打开或关闭自动提交数据库修改
- mysqli_release_savepoint() - 从当前事务的一组保存点中删除指定的保存点
← mysqli::release_savepoint
mysqli::rpl_query_type →