zoukankan      html  css  js  c++  java
  • PDO

    <?
    //数据源
    $dsn ='mysql:host=localhost;dbname=sankea';
    //用户名
    $username='root';
    //密码
    $password='root';

    try{
    //开启事务
    $pdo->beginTransaction();//执行start transaction;sql语句
    //给ID=1 加1000
    $sql='UPDATE CS SET amount= amount+1000 WHERE id= 1';
    $pdo->exec($sql);//执行sql
    //给ID=2 减1000
    $sql='UPDATE CS SET amount= amount-1000 WHERE id= 2';
    $pdo->exec($sql);//执行sql
    //事务提交
    $pdo->commit();//执行commit;sql语句
    }catch (PDOException $ex){
    //
    echo'<br>回调';
    $pdo->rollback();
    echo'<br>错误消息:'.$ex->getMessage();
    echo'<br>错误文件:'.$ex->getFile();
    echo'<br>错误行号:'.$ex->getLine();
    }





    try{
    //如果try中的代码块,有错误,就去执行catch语句
    $pdo= new PDO($dsn,$username,$password);
    //PDO报错方式
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

    $sql='select * from cs where id=1';
    $stmt=$pdo->query($sql);

    }catch(PDOException $e){//相当于$e= new PDOException();
    echo'<br>错误消息:'.$e->getMessage();
    echo'<br>错误文件:'.$e->getFile();
    echo'<br>错误行号:'.$e->getLine();
    }

  • 相关阅读:
    Python #面向对象
    Python #@property属性
    Linux # screen 用法
    Shell #监控进程脚本
    Linux # nethogs
    Python #logging
    Python #time
    Python # 一个api接口调用POST请求
    Python # 和风天气接口
    IP地址的组成
  • 原文地址:https://www.cnblogs.com/yucaiyun/p/6868581.html
Copyright © 2011-2022 走看看