Home  >  Article  >  Backend Development  >  Instances of linked database wrapper classes

Instances of linked database wrapper classes

一个新手
一个新手Original
2017-10-24 09:01:301357browse

<?php
class DBDA
{
    public $host="localhost";
    public $uid="root";
    public $pwd="root";
    public $dbname="db_0808";
    /**
     *给一个sql语句,返回执行的结果
     *@param string $sql 用户指定的sql语句
     *@param int $type 用户给的语句类型,0代表增删改,1代表查询
     *@return  返回查询的结果,如果是查询返回二维数组,如果是增删改返回true或false
     */
    function Query($sql,$type=1)
    {
        //造连接对象
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
        //执行sql语句
        $reslut = $db->query($sql);
       if(!$reslut){
           die($db->error);
            }
//从结果集对象里面取数据
        if($type==1)
        {
            return $reslut->fetch_all();
        }
        else
        {
            return $reslut;
        }
    }
    /**
     *给一个sql语句,返回关联的二维数组
     *@param string $sql 用户指定的sql语句
     *@param int $type 用户给的语句类型,0代表增删改,1代表查询
     *@return  返回查询的结果,如果是查询返回二维数组,如果是增删改返回true或false
     */
    function GuanQuery($sql,$type=1)
    {
        //造连接对象
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
        //执行sql语句
        $reslut = $db->query($sql);
        //取数据
        if($type==1)
        {
            $attr = array();
            while($a = $reslut->fetch_assoc())
            {
                $attr[] = $a;
            }
            return $attr;
        }
        else
        {
            return $reslut;
        }
    }
    /**
     *给一个sql语句,返回字符串
     *@param string $sql 用户指定的sql语句
     *@param int $type 用户给的语句类型,0代表增删改,1代表查询
     *@return  返回查询的结果,如果是查询返回字符串,如果是增删改返回true或false
     */
    function StrQuery($sql,$type=1)
    {
        //造连接对象
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
        //执行sql语句
        $reslut = $db->query($sql);
        //取数据
        if($type==1)
        {
            $attr = $reslut->fetch_all();
            $str="";
            foreach($attr as $v)
            {
                $str .= implode("^",$v);
                $str .="|";
            }
            return substr($str,0,strlen($str)-1);
        }
        else
        {
            return $reslut;
        }
    }
}

The above is the detailed content of Instances of linked database wrapper classes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn