在mysql 5中,开始支持事务了,在使用中要搭配mysqli来进行,有一定的模式,现小结如下
<?
// Instantiate the mysql class
$mysqldb = new mysqli("localhost","root","xxx","xxx");
// Start by assuming the transaction operations will all succeed
$success = TRUE;
// Disable the autocommit feature,设置手动事务
$mysqldb->autocommit(FALSE);
$query = "UPDATE employee SET cash=cash-$price WHERE rowID='$buyerid'";
$result = $mysqldb->query($query);
if (!$result OR $mysqldb->affected_rows != 1 )
$success = FALSE;
.....
if ($success) {
$mysqldb->commit();
echo "事务成功";
} else {
$mysqldb->rollback();
echo "事务失败";
}