• 事物回滚


    <?php
    /**
    * Created by PhpStorm.
    * User: song tong jing
    * Date: 2018/11/5
    * Time: 15:30
    */
    header("content-type:text/html;charset=utf-8");
    try{
    $pdo = new PDO("mysql:host=127.0.0.1;dbname=student","root","root");
    $pdo->exec("set names utf8");
    }catch (PDOException $exc){
    echo $exc->getTraceAsString();//获取字符串的错误信息
    }

    //事务回滚
    $pdo->beginTransaction();//开启事务
    try{
    $price_sql="select price from goods where g_id=1";//搜索出来价格
    $res=$pdo->query($price_sql)->fetch();
    $price=intval($res['price']);
    $sql1="update member set money=money-$price where men_id=1";
    $result = $pdo->exec($sql1);
    if(!$result)
    {
    throw new Exception("张三钱包减钱失败");
    }
    $sql2="update members set money=money+$price where men_id=1";
    $result=$pdo->exec($sql2);
    if(!$result)
    {
    throw new Exception("李四钱包加钱失败");
    }

    $sql3="update exam_goods set number=number-1 where g_id=1";//修改库存
    $result=$pdo->exec($sql3);
    if(!$result)
    {
    throw new Exception("库存减1失败");
    }
    $pdo->commit();//事物自动提交
    }catch(Exception $ex){
    $pdo->rollBack();//回滚
    echo $ex->getMessage();//得到错误信息
    }
    你所浪费的今天是那些死去的人所奢望的明天,你所厌恶的现在是未来的你所回不去的曾经。
  • 相关阅读:
    2017.4.18下午
    2017.4.18上午
    2017.4.17上午
    2017.4.14下午
    2017.4.14上午
    4.17下午
    4.17上午
    4.13下午
    4.13上午
    4.10上午
  • 原文地址:https://www.cnblogs.com/stj123/p/9921203.html
Copyright © 2020-2023  润新知