PDO事务实现
header("Content-Type:text/html;charset=utf-8");
$dsn = "mysql:host=127.0.0.1;dbname=php_03;port=3306;charset=utf8";
$username = 'root';
$password = 'root';
$pdo = new PDO($dsn, $username, $password);
//1. 开启事务
$pdo -> beginTransaction();
//2, 从宋江的账号扣钱
$sql = "UPDATE cash SET money=money-5000 WHERE id=2";
$result = $pdo -> exec($sql);
//3. 李逵的账号加钱
$sql = "UPDATE cash SET money=money+5000 WHERE id=1";
$result2 = $pdo -> exec($sql);
if($result && $result2){
//提交
$pdo -> commit();
echo '交易成功!';
}else{
//回滚
$pdo -> rollBack();
echo '交易失败!';
}
以上内容希望帮助到大家,更多PHP大厂PDF面试文档,PHP进阶架构视频资料,PHP精彩好文可以关注公众号:PHP开源社区,或者访问:
来源:https://www.cnblogs.com/lovebing/p/12108028.html