PHP事务实现原理

2021-05-05 11:27

阅读:654

标签:rda   sql   utf8   charset   blog   port   llb   公众号   ash   

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事务实现原理

标签:rda   sql   utf8   charset   blog   port   llb   公众号   ash   

原文地址:https://www.cnblogs.com/lovebing/p/12108028.html


评论


亲,登录后才可以留言!