• PDO对象


    <?php
    //造DSN:驱动名:dbname=数据库名;host=服务器地址
    $dsn = "mysql:dbname=mydb;host=localhost";
    //造PDO对象
    $pdo = new PDO($dsn,"root","123");
    
    //写SQL语句
    $sql = "select * from Info";
    
    
    //预处理,先把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 "执行失败!";
    }
    
    ?>
    </body>
    </html>
     

    pdo采用数组调用

    <body>
    <?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))//接受form 表单中的数据
    {
        echo "添加成功";
    }
    else
    {
        echo "添加失败";
    }
    
    
    
    ?>
    
  • 相关阅读:
    go-go协程
    linux-pclint代码检测
    linux-32位-交叉编译openssl
    go-json类
    mysql-定时任务
    go-IO操作
    go-异常处理-error-panic-recover
    go-defer语句
    go-select
    go-指针
  • 原文地址:https://www.cnblogs.com/kevin2016/p/5496190.html
Copyright © 2020-2023  润新知