• pdo 访问数据库


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    
    <body>
    
    <?php
    
    PDO
    1.访问不同的数据库
    2.自带事务功能
    3.防止SQL注入
    
    1.造对象
    $dsn = "mysql:dbname=mydb;host=localhost";
    $pdo = new PDO($dsn,"root","123");
    
    2.写SQL语句
    $sql = "update nation set name='兽族' where code='n013'";
    
    3.执行SQL语句
    $r = $pdo->query($sql);
    $r = $pdo->exec($sql);*/
    
    事务功能
    造对象
    $dsn = "mysql:dbname=mydb;host=localhost";
    $pdo = new PDO($dsn,"root","123");
    
    设置异常模式
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    
    
    写SQL语句
    $sql1 = "insert into nation values('n016','人族')";
    $sql2 = "insert into nation values('n017','不死族')";
    
    执行两条SQL语句
    try
    {
        //启动事务
        $pdo->beginTransaction();
        
        $pdo->exec($sql1);
        $pdo->exec($sql2);
        
        提交事务
        $pdo->commit();
    }
    catch(PDOException $e)
    {
        $e->getMessage();
        回滚
        $pdo->rollBack();
    }
    
    ?>
    
    </body>
    </html>
  • 相关阅读:
    xmlHttpRequest参数
    蒙哥马利:“沙漠之狐”猎手
    c# as is 类型转换
    值类型和引用类型
    BuuctfmiscN种方法解决
    Buuctfmisc二维码
    Buuctfweb[HCTF 2018]WarmUp
    Buuctfmisc大白
    map用索引作下标之后,再插值时报错.
    c++ stl 的string 的size() legth()区别
  • 原文地址:https://www.cnblogs.com/benpaodegegen/p/6039510.html
Copyright © 2020-2023  润新知