编程学习网 > PHP技术 > php高级 > PHP事务实现原理
2020
08-19

PHP事务实现原理

attachments-2020-08-MyhkkfR95f3c9a98319a8.png



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 '交易失败!';

    }


扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取