封装php连接数据库返回方法

<?php

class fengzhuang
{
    public $host="localhost";
    public $uid="root";
    public $pwd="159357";
    public $sjkname="yuheng";


//返回二维数组    
    function query($sql,$type=1)
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->sjkname);
        
        $reslut = $db->query($sql);
        
        if($type==1)
        {
            return $reslut->fetch_all();
        }
        else
        {
            return $reslut;
        }
    }



//返回关联数组
    
    function glquery($sql,$type=1)
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->sjkname);
        
        $reslut = $db->query($sql);
        
        if($type==1)
        {
            $attr = array();
            while($a = $reslut->fetch_assoc())        //取一条
            {
                $attr[] = $a;
            }
            return $attr;
            
        }
        else
        {
            return $reslut;
        }
    }
    
    
    
//返回接字符串
    
    function strquery($sql,$type=1)
    {
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->sjkname);
        
        $reslut = $db->query($sql);
        
        if($type=1)
        {
            $attr = $reslut->fetch_all();        //二维数组不能用implode
            
            $str = "";
            foreach($attr as $v)
            {
                $str .= implode("^",$v);
                $str .= "|";
            }
            return substr($str,0,strlen($str)-1);
        }
        else
        {
            return $reslut;
        }
    }
}

 

posted @ 2017-02-19 18:09  厚爱无需我多言。  阅读(255)  评论(0编辑  收藏  举报