定义和用法
mysqli_rollback - 回退当前事务
语法:
mysqli_rollback ( mysqli $link )
参数
参数 | 必需的 | 描述 |
---|---|---|
link | 是 | 由mysqli_connect() 或 mysqli_init() 返回的链接标识。 |
示例
$link = mysqli_connect("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s ", 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. ", $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). ", $row[0]); /* Free result */ mysqli_free_result($result); } /* Drop table myCity */ mysqli_query($link, "DROP TABLE myCity"); mysqli_close($link);
相关函数
mysqli_commit() - 提交一个事务
mysqli_autocommit() - 打开或关闭本次数据库连接的自动命令提交事务模式