Learn to present your own encapsulated DB class and PDO class

Source: Internet
Author: User
Tags dsn prepare

DB Package Class

<?PHPclassdbda{ Public $host= "localhost";  Public $uid= "Root";  Public $pwd= "Root";  Public $dbname= "MyDB";  Public functionQuery ($sql,$type=1) //Connect to the database, the parameter defaults to 1 when the query results, the other for the additional deletion.     {        $db=NewMysqli ($this->host,$this->uid,$this->pwd,$this-dbname); $result=$db->query ($sql); if($type= = "1")        {            return $result-Fetch_all (); }        Else        {            return $result; }    }         Public functionStrquery ($sql,$type=1) //This method is used for string concatenation of the extracted data (two-dimensional array) , separated by ^ and |     {        $db=NewMysqli ($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 functionJsonquery ($sql,$type=1) //This method is used in Ajax to return the Jason data type when using {$db=NewMysqli ($this->host,$this->uid,$this->pwd,$this-dbname); $result=$db->query ($sql); if($type= = "1")        {            $arr=$result-Fetch_all (MYSQLI_ASSOC); //In parentheses to change parameters to associative arrays returnJson_encode ($arr); }        Else        {            return $result; }    }}

PDO Package Class

<?PHPclassdbdap{ Public $host= "localhost";  Public $uid= "Root";  Public $pwd= "Root";  Public $dbname= "MyDB";  Public functionQuery ($sql,$type=1)    {        $dsn= "Mysql:dbname=$this->dbname;host=$this->host "; $pdo=NewPDO ($dsn,"$this->uid ","$this->pwd "); $result=$pdo->query ($sql); if($type= = "1") //parameter is 1 o'clock, return query result {return $result->fetchall (PDO::FETCH_ASSOC); //The parameters in parentheses must be filled in, otherwise the data obtained will be duplicated. If not written, the returned data also has index data associated with the data.         }        Else        {            $zeng=$pdo->prepare ($sql); if($type= = "2") ///parameter is 2 o'clock, you can make a preprocessing statement {return $zeng; }Else            {            return $result; }        }    }         Public functionStrquery ($sql,$type=1) //This method is used to stitch the extracted data (two-dimensional array) into string processing {$dsn= "Mysql:dbname=$this->dbname;host=$this->host "; $pdo=NewPDO ($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 functionJsonquery ($sql,$type=1) //This method is used in Ajax to return to the Jason data type when using {$dsn= "Mysql:dbname=$this->dbname;host=$this->host "; $pdo=NewPDO ($dsn,"$this->uid ","$this->pwd "); $result=$pdo->query ($sql); if($type= = "1")        {            $arr=$result->fetchall (PDO::FETCH_ASSOC); returnJson_encode ($arr); }        Else        {            $zeng=$pdo->prepare ($sql); if($type= = "2")            {            return $zeng; }            Else            {            return $result; }        }    }    }

Learn to present your own encapsulated DB class and PDO class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.