首页 > 代码库 > 学习到目前,自己封装的db类和pdo类

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

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类