• 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
    //造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 "执行失败!";
    }

    ?>
    </body>
    </html>

    <!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

    //造对象
    $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 "添加失败";
    }

    ?>
    </body>
    </html>

    自己打的配置工具

    <?php
    class pdoa
    {
    public $host = "localhost"; //数据库地址
    public $uid = "root"; //数据库用户名 、
    public $pwd = ""; //数据库密码

    //执行SQL语句,返回相应的结果的方法
    //参数:$sql代表要执行的SQL语句,$type是SQL语句类型0代表查询1代表其他,$db代表要操作的数据库
    public function Prepare($sql,$db="bcs")
    {
    $dsn = "mysql:dbname=$db;host=$this->host";
    $pdo = new PDO($dsn,$this->uid,$this->pwd);


    //3.执行SQL语句
    $stm = $pdo->prepare($sql);

    if($stm->execute())
    {
    return $stm->fetchAll();
    }
    else
    {
    echo "连接失败";
    }

    }
    }

  • 相关阅读:
    jar包和war包的介绍和区别
    java中getAttribute和getParameter的区别
    修改tomcat默认的编码方式
    jQuery遮罩插件jQuery.blockUI.js简介
    Sql Server 2008 Management studio安装教程
    评论字数限制和字数显示
    如何将表单元素封装
    DWR原理探秘
    linux命令详解:pgrep命令
    使用Nginx实现灰度发布
  • 原文地址:https://www.cnblogs.com/xiongxiaobai/p/5492188.html
Copyright © 2020-2023  润新知