MYSQL事务处理让所有sql语句执行成功后才去处理,如果有一条没有成功或者报错就会回滚事务,防止敏感操作处理失败。MYSQL中只有INNODB和BDB类型的数据表才能支持事务处理!其它类型是不支持的!
$mysqli->autocommit(FALSE); //关闭自动提交功能 $sql = "UPDATE users SET money = money-300 WHERE username = 'Mike'"; $res1 = $mysqli->query($sql); $res1_rows = $msyqli->affected_rows; $sql2 = "UPDATE users SET money = money+300 WHERE username = 'Tom'"; $res2 = $mysqli->query($sql2); $res2_rows = $mysqli->affected_rows; if($res1 && $res1_rows>0 && $res2 && $res2_rows>0){ $msyqli->commit(); //提交事务 echo '执行成功'; $mysqli->autocommit(TRUE); //开启自动提交功能 else{ //事务回滚 $mysqli->rollback(); echo '执行失败; }