PDO事务实现
<?php
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 '交易失败!';
}
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
__________________________________________________________________________________
若有帮助到您,欢迎捐赠支持,您的支持是对我坚持最好的肯定(*^_^*)