zoukankan      html  css  js  c++  java
  • 封装类的方式访问数据库(封装字符串、json)

    <?php
    class DBDA
    {
        public $host="localhost";//服务器地址
        public $uid="root";//用户名
        public $pwd="";//密码
        
        public $conn;//连接对象
        //操作数据库的方法
        //$sql代表需要执行的SQL语句
        //$type代表SQL语句的类型,1代表查询,0代表增删改
        //$db代表要操作的数据库名称
        //如果是查询,返回二维数组
        //如果是其他语句,返回true或false
        function __construct($db="mydb")
        {
            //造连接对象
            $this->conn = new MySQLi($this->host,$this->uid,$this->pwd,$db);
            //一个属于这个类的对象不能直接调用另一个类的成员,可以在此类中创建那个类的成员变量,该对象调用此成员变量,再调用那个类的方法
        }
        public function Query($sql,$type=1){
            //判断是否出错
            !mysqli_connect_error() or die("连接失败!");
            //执行SQL语句
            $result = $this->conn->Query($sql);
            //判断SQL语句类型
            if($type==1)        {
                //如果是查询语句,返回结果集的二维数组
                return $result->fetch_all();
            }else{
                //如果是其他语句,返回true或false
                return $result;
            }
        }
        
        //Ajax调用返回JSON
        public function JsonQuery($sql,$type=1,$db="mydb"){
            //定义数据源
            $dsn = "mysql:dbname={$db};host={$this->host}";
            //造pdo对象
            $pdo = new PDO($dsn,"{$this->uid}","{$this->pwd}");
            //准备执行SQL语句
            $st = $pdo->prepare($sql);
            //执行预处理SQL语句
            if($st->execute()){
                if($type==1){
                    $sttr = $st->fetchAll(PDO::FETCH_ASSOC);
                    return json_encode($attr);
                }else{
                    if($st){
                        return "OK";
                    }else{
                        return "NO";
                    }
                }
            }else{
                echo "执行失败!";
            }
        }
        
        //Ajax调用返回字符串
        public function StrQuery($sql,$type=1){
            //判断连接是否成功
            !mysqli_connect_error() or die("连接失败!");
            //执行SQL语句
            $result = $this->conn->query($sql);
            //判断SQL语句类型
            if($type==1){
                $attr = $result->fetch_all();
                $str = "";
                //如果是查询语句返回字符串
                for($i=0;$i<count($attr);$i++){
                    for($j=0;$j<count($attr[$i]);$j++){
                        $str = $str.$attr[$i][$j];
                        $str = $str."^";
                    }
                    $str = substr($str,0,strlen($str)-1);
                    $str = $str."|";
                }
                $str = substr($str,0,strlen($str)-1);
                return $str;
            }else{
                //如果是其他语句,返回true或false
                if($result){
                    return "OK";
                }else{
                    return "NO";
                }
            }
        }
        
        function PdoQuery($sql,$type=1,$db="mydb"){
            //造数据源
            $dns = "mysql:host={$this->host};dbname={$db}";
            //造pdo对象
            $pdo = new PDO($dns,$this->uid,$this->pwd);
            //准备一条SQL语句
            $stm = $pdo->prepare($sql);
            //执行预处理语句
            $r = $stm->execute();
            if($r){
                if($type==1){
                    return $stm->fetchAll();
                }else{
                    return "OK"; 
                }
            }else{
                return "NO";
            }
        }
    }
  • 相关阅读:
    在新浪爱问上看到的有趣名字都记下来
    FastReports_4.14.1 _Cliff手动安装
    计算机算法对做事效率的启发(既要高强度猛攻,也要细水长流)
    有趣的数组
    listView 分页加载数据
    如何隐藏Cognos Viewer
    使用DBUnit实现对数据库的测试
    通过Jasmine和Guard自动测试JavaScript
    for惠普2013实习生
    栈的顺序存储,所谓的顺序栈
  • 原文地址:https://www.cnblogs.com/jinshui/p/5627693.html
Copyright © 2011-2022 走看看