zoukankan      html  css  js  c++  java
  • php课程---初学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
    
        try{
        $dsn = "mysql:dbname=mydb;host=localhost";
        $pdo = new PDO($dsn,"root","123");
        }
        catch(PDOException $e)
        {
            echo "连接出错".$e->getMessage();
        }
        
        
        $sql = "select * from Info";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();    
         
        while($row = $stmt->fetch())
        {
            print_r($row);
            echo "<br>";
        }
        
        
        //PDO预处理
        //写好SQL语句
        $sql = "insert into Info values(?,?,?,?,?)";
        //把SQL语句放在服务器上等待执行
        $stmt = $pdo->prepare($sql);
        
        //将SQL语句中的?绑定上参数
        $stmt->bindParam(1,$code);
        $stmt->bindParam(2,$name);
        $stmt->bindParam(3,$sex);
        $stmt->bindParam(4,$nation);
        $stmt->bindParam(5,$birthday);
        
        //给参数赋值
        $code = "p11";
        $name = "王五";
        $sex = true;
        $nation = "n001";
        $birthday = "1989-2-3";
        
        //执行
        $stmt->execute();
        
        //执行
        $stmt -> execute(array("p111", '赵六', false, 'n002', '1989-3-4')); */
        
        //$sql = "insert into Info values(:code, :name, :sex, :nation, :birthday)";
        
        //$stmt = $pdo->prepare($sql);
        
        $stmt->bindParam("code", $code, PDO::PARAM_STR);
        $stmt->bindParam("name", $name, PDO::PARAM_STR);
        $stmt->bindParam("sex", $sex, PDO::PARAM_STR);
        $stmt->bindParam("nation", $nation, PDO::PARAM_STR);
        $stmt->bindParam("birthday", $birthday, PDO::PARAM_STR);
        
        $code = "p112";
        $name = "王五";
        $sex = true;
        $nation = "n001";
        $birthday = "1989-2-3";
        
        $stmt->execute();*/
        
        //$stmt->execute(array("code"=>"p020","name"=>"随便","sex"=>true,"nation"=>"n003","birthday"=>"1988-5-6"));
        
        
        
        
    
    ?>
    </body>
    </html>
  • 相关阅读:
    C#(16进制数据之间的转换)
    解决windows7您当前无权访问该文件夹的问题
    获取本周的周一和周日
    在GridView中使用Cache
    在C#中完成海量数据的批量插入和更新
    母版页中对控件ID的处理
    PostgreSQL函数和运算符(二)
    js检查首字符是否是英文字符
    Asp.net Ajax实现拖放实战
    js移除数组中指导位置元素
  • 原文地址:https://www.cnblogs.com/0927wyj/p/5205436.html
Copyright © 2011-2022 走看看