• PDO


    <?php
    //造DSN:驱动名:dbname=数据库名;host=服务器地址
    $dsn = "mysql:dbname=mydb;host=localhost";
    //造PDO对象
    $pdo = new PDO($dsn,"root","123");
    
    //写SQL语句
    $sql = "select * from Info";
    
    //执行SQL语句
    //$v = $pdo->query($sql);
    //返回的对象比较特殊,可以直接用循环遍历
    /*foreach($v as $a)
    {
        echo $a[0];
    }*/
    
    //预处理,先把SQL语句放到服务器上等待我们给他指令去执行
    //准备一条SQL语句
    $stm = $pdo->prepare($sql);
    
    //执行准备好的SQL语句,成功返回true,失败返回false
    if($stm->execute())
    {
        //1.逐行取数据
        //var_dump($stm->fetch());
        //2.取所有的
        //var_dump($stm->fetchAll(PDO::FETCH_OBJ));
        //3.取某一列
        //var_dump($stm->fetchColumn(1));
        //4.返回对象
        //var_dump($stm->fetchObject());
        
    }
    else
    {
        echo "执行失败!";
    }
    
    ?>
    <?php
    
    //造对象
    $dsn = "mysql:dbname=mydb;host=localhost";
    $pdo = new PDO($dsn,"root","123");
    
    //写SQL语句,参数用?来代替
    //$sql = "insert into Nation values(?,?)";
    $sql = "insert into Nation values(:code,:name)";
    //预处理
    $stm = $pdo->prepare($sql);
    
    //给参数绑定值
    /*$stm->bindParam(1,$code);
    $stm->bindParam(2,$name);
    $code = "p100";
    $name = "回家";*/
    
    /*$stm->bindParam("code",$code,PDO::PARAM_STR);
    $stm->bindParam("name",$name,PDO::PARAM_STR);
    $code = "p102";
    $name = "回家";*/
    
    //造数组
    //$attr  = array("p101","很快");
    //$attr  = array("code"=>"p103","name"=>"shj");
    
    //执行
    if($stm->execute($_POST))
    {
        echo "添加成功";
    }
    else
    {
        echo "添加失败";
    }
    
    ?>
  • 相关阅读:
    chrome headless+selenium+python+(ubuntu 16.04/centos7) 下的实现
    selenium + phantomJS 常用方法总结
    Rabbitmq consumer端超时报错
    python 守护进程
    如何在GitBook中使用Git管理
    Java#Spring框架下注解解析
    Linux 之Ubuntu在VM中安装(桌面版)
    Linux----Ubuntu虚拟机(VMWare)学习
    Python tuple元组学习
    Python 编解码
  • 原文地址:https://www.cnblogs.com/zhaimiaoer/p/5522709.html
Copyright © 2020-2023  润新知