zoukankan      html  css  js  c++  java
  • mysql事务处理

    事务处理能保证所有的sql操作一次性完成或回滚,mysql默认的MyISAM表类型是不支持事务处理的,如果需要做事务处理,需要把表类型换成InnoDB

    <?php 
    $dsn='mysql:host=localhost;dbname=mssc';
    $user='root';
    $password='';
    $orderno='111';
    $orderno1='222';
    try {
        $dbh=new PDO($dsn,$user,$password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 
        $dbh->beginTransaction();
        $sql="INSERT INTO onethink_order(order_no) VALUES(:orderno)";
        $stmt=$dbh->prepare($sql);
        $stmt->bindParam(':orderno',$orderno);
        $stmt->execute();
        $sql="INSERT INTO onethink_order(order_no) VALUES(:orderno1)";
        $stmt=$dbh->prepare($sql);
        $stmt->bindParam(':orderno1',$orderno1);
        $stmt->execute();
        $dbh->commit();
    } catch (PDOException $e) {
        $dbh->rollBack();
        echo 'SQL Query:'.$sql.'</br>';
        echo 'Connection failed:'.$e->getMessage();
    }
     ?>

    如果由于某种原因导致第二条sql没有执行,那整个操作将回滚到之前状态

  • 相关阅读:
    c#截取后几位
    GridView里嵌套RadioButton单选
    sql存储过程无重复添加修改
    Javascript指令
    多级联动Dropdownlist(刷新版)
    docker部署redis问题解决
    docker 安装 gitlab
    docker安装jenkins
    部分ansible常用模块
    ansible
  • 原文地址:https://www.cnblogs.com/hltswd/p/6241596.html
Copyright © 2011-2022 走看看