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

<?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;
        }
    }
}

 

posted @ 2017-04-25 09:30  君陌丶  阅读(252)  评论(0编辑  收藏  举报