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