封装一个连接数据库的DBDA类。

时间:2021-12-22 20:26:23
<?php
class DBDA
{
    public $host = "localhost";
    public $uid = "root";
    public $pwd = "123456";
    public $dbname = "youmuren";

    public function Query($sql,$type=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)."|";

            }
           return substr($str,0,strlen($str)-1);
        }else
        {
            return $result;
        }
    }
    public function JsonQuery($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(MYSQLI_ASSOC);
            return json_encode($arr);
        }else
        {
            return $result;
        }
    }
}