zoukankan      html  css  js  c++  java
  • 事物回滚

    <?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();//得到错误信息
    }
    你所浪费的今天是那些死去的人所奢望的明天,你所厌恶的现在是未来的你所回不去的曾经。
  • 相关阅读:
    Js
    CSS
    CSS
    第七周作业及总结
    第六周作业及总结
    第五周作业及总结
    第四周作业及总结
    第三周作业及总结
    7-1 判断上三角矩阵
    第二周作业及总结
  • 原文地址:https://www.cnblogs.com/stj123/p/9921203.html
Copyright © 2011-2022 走看看