<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>测试事务处理</title>
</head>
<body>
<?php
$link = mysqli_connect("localhost", "root", "", "my_db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* disable autocommit */
mysqli_autocommit($link, FALSE);
mysqli_query($link, "update T_test set Ttype='9' where Id='2'");
/* commit insert */
mysqli_commit($link);
/* delete all rows */
//mysqli_query($link, "DELETE FROM myCity");
if ($result = mysqli_query($link, "SELECT COUNT(*) FROM T_test where Ttype='111'")) {
$row = mysqli_fetch_row($result);
printf("%d rows in table test.\n", $row[0]);
/* Free result */
mysqli_free_result($result);
}
/* Rollback */
mysqli_rollback($link);
if ($result = mysqli_query($link, "SELECT COUNT(*) FROM T_test where Ttype='111'")) {
$row = mysqli_fetch_row($result);
printf("%d rows in table test (after rollback).\n", $row[0]);
/* Free result */
mysqli_free_result($result);
}
/* Drop table myCity */
//mysqli_query($link, "DROP TABLE myCity");
mysqli_close($link);
?>
</body>
</html>