码迷,mamicode.com
首页 > 数据库 > 详细

学习到目前,自己封装的db类和pdo类

时间:2017-03-19 14:06:09      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:font   logs   style   拼接   new   strlen   oca   etc   括号   

DB封装类

<?php
class DBDA
{
    public $host = "localhost";
    public $uid = "root";
    public $pwd = "root";
    public $dbname = "mydb";
    
    public function Query($sql,$type=1)  //连接数据库,参数默认为1的时候为查询结果,其它的为增删改。
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
        $result = $db->query($sql);
        
        if($type=="1")
        {
            return $result->fetch_all();
        }
        else
        {
            return $result;
        }
    }
    
    public function StrQuery($sql,$type=1)  //此方法用于把取出的数据(二维数组)进行字符串拼接处理,用^和|分隔开。
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
        $result = $db->query($sql);
        
        if($type=="1")
        {
            $arr = $result->fetch_all();
            $str = "";
            foreach($arr as $v)
            {
                $str = $str.implode("^",$v)."|";
            }
            $str = substr($str,0,strlen($str)-1);
            return  $str;
        }
        else
        {
            return $result;
        }
    }
    
    public function JsonQuery($sql,$type=1)  //此方法用于ajax中返回的是jason数据类型时使用
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
        $result = $db->query($sql);
        
        if($type=="1")
        {
            $arr = $result->fetch_all(MYSQLI_ASSOC);//括号内为将参数改为关联数组
            return json_encode($arr);
        }
        else
        {
            return $result;
        }
    }
}

 

PDO封装类

<?php
class DBDAP
{
    public $host = "localhost";
    public $uid = "root";
    public $pwd = "root";
    public $dbname = "mydb";
    
    public function Query($sql,$type=1)
    {
        $dsn = "mysql:dbname=$this->dbname;host=$this->host";
        $pdo = new PDO($dsn,"$this->uid","$this->pwd");
        $result = $pdo->query($sql);
        
        if($type=="1")  //参数为1时,返回查询结果
        {
            return $result->fetchall(PDO::FETCH_ASSOC);//括号内的参数必须填写,不然取得的数据会发生重复现象。若不写,返回的数据有关联数据也有索引数据。
        }
        else
        {
            $zeng = $pdo->prepare($sql);
            if($type=="2")  //参数为2时,可以进行预处理语句
            {
            return $zeng;
            }else
            {
            return $result;
            }
        }
    }
    
    public function StrQuery($sql,$type=1)  //此方法用于对取出的数据(二维数组)进行拼接字符串处理
    {
        $dsn = "mysql:dbname=$this->dbname;host=$this->host";
        $pdo = new PDO($dsn,"$this->uid","$this->pwd");
        $result = $pdo->query($sql);
        
        if($type=="1")
        {
            $arr = $result->fetchall(PDO::FETCH_ASSOC);
            $str = "";
            foreach($arr as $v)
            {
                $str = $str.implode("^",$v)."|";
            }
            $str = substr($str,0,strlen($str)-1);
            return  $str;
        }
        else
        {
            $zeng = $pdo->prepare($sql);
            if($type=="2")
            {
            return $zeng;
            }
            else
            {
            return $result;
            }
        }
    }
    public function JsonQuery($sql,$type=1)  //此方法用于ajax中用于返回为jason数据类型时使用
    {
        $dsn = "mysql:dbname=$this->dbname;host=$this->host";
        $pdo = new PDO($dsn,"$this->uid","$this->pwd");
        $result = $pdo->query($sql);
        
        if($type=="1")
        {
            $arr = $result->fetchall(PDO::FETCH_ASSOC);
            return json_encode($arr);
        }
        else
        {
            $zeng = $pdo->prepare($sql);
            if($type=="2")
            {
            return $zeng;
            }
            else
            {
            return $result;
            }
        }
    }
    
}

 

学习到目前,自己封装的db类和pdo类

标签:font   logs   style   拼接   new   strlen   oca   etc   括号   

原文地址:http://www.cnblogs.com/gaobint/p/6579873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!