leave a footprint
<?php class pdodb{ //public protected private //In this class, the connection will not be established without executing the SQL command. //And after execution, there is an operation to close the connection and clear the result. private $pdodb;//connection private $res;//Remote result object public $arr;//Local result array private $cs=array(//connect the array 'host' =>'127.0.0.1', 'port' =>'3306', 'username' =>'root', 'password' =>'', 'dbname' =>'mysql', 'charset' =>'utf8', ); //********************************* //****************Singletonization************ //********************************* //Three private and one public private function __construct(){}//Invalidate new() new object private function __clone(){}//Invalidate the cloned object. private static $db;//Singleton object property public static function getdb($arr=null)//Singleton object entry { if(!isset(self::$db)) { self::$db=new self(); if($arr){ if(isset($arr['host']))self::$db->cs['host']=$arr['host']; if(isset($arr['port']))self::$db->cs['port']=$arr['port']; if(isset($arr['username']))self::$db->cs['username']=$arr['username']; if(isset($arr['password']))self::$db->cs['password']=$arr['password']; if(isset($arr['dbname']))self::$db->cs['dbname']=$arr['dbname']; if(isset($arr['charset']))self::$db->cs['charset']=$arr['charset']; } } return self::$db; } //connect private function connect(){ // //connection information $dsn="mysql:host=".$this->cs['host'].//主机 ";port=".$this->cs['port'].//Port ";dbname=".$this->cs['dbname'];//Database name //login and password $username=$this->cs['username']; $password=$this->cs['password']; //connect operation try{ $this->pdodb = new PDO($dsn,$username,$password); }catch (PDOException $e) { echo "<br>Connection failed error:new PDO()---PDO:58 "; echo "<br>". $e->getMessage() ; die("<br>STOP!!!"); } return $this->pdodb; } //set character set private function charset($char='utf8'){ $this->pdodb->query("set names ".$char); } //USE database private function usedb($dbname='mysql'){ $this->pdodb->query("use ".$dbname); } // database preparation private function ready(){ $this->charset($this->cs['charset']); $this->usedb($this->cs['dbname']); } //Execute the SQL command //$sql---command:string //$erro----Error switch: any value that is not NULL 0123.... private function query($sql,$erro=null){ $this->connect(); $this->ready(); $this->res = $this->pdodb->query($sql); if($erro!==null){ if(!$this->res){ $error=$this->pdodb->errorInfo(); echo "<br>****************************"; echo "<br>Failed to execute"; echo "<br>命令:".$sql; echo "<br>Customer code:".$error[0]; echo "<br>Error code:".$error[1]; echo "<br>Error message:".$error[2]; echo "<br>****************************"; die("<br>STOP!!!"); } } return $this->res; } // public command execution with return //The return result is stored in $this-arr //Result type array true/false three kinds //parameter: //$sql---MYSQL command //$index--- returns the subscript type of the data, the default is the field subscript // type: 'n'---numeric subscript // 'a'---Field subscript // 'b'---value/field (two kinds) double subscript public function queryarr($sql=null,$index='a'){ if($sql==null){return $this->arr=false;} //$r=$this->query($sql,0);//Add the second parameter to open the error $this->query($sql); // There are only three cases for the result object //1. Executed correctly, but did not return a result set.---Boolean: true //2. Execution failed---Boolean: false //3. Correct execution, there is a return result, return an object----object:object // The result can be retrieved from the object and stored in a local array //Note one case, that is to return an empty array.array{} if($this->res===true){ $this->arr=true; }else{ if($this->res===false){ $this->arr=false; }else{ //Get all data three types of subscripts switch ($index) { case 'a': $this->arr=$this->res->fetchAll(PDO::FETCH_ASSOC);//Field subscript break; case 'n': $this->arr=$this->res->fetchAll(PDO::FETCH_NUM);//Numerical subscript break; case 'b': $this->arr=$this->res->fetchAll();//Double subscript break; } $this->res->closeCursor();//Release the result object } } //$this->pdodb->close();//Close the connection object $this->pdodb=null;//How to reset PDO to close the connection object $this->res=null;//After resetting the release result, the object still exists. So reset return $this->arr; } //********************** //Pure output data to table table public function tab($arr) { if($arr===true)$arr=array(array("true")); if($arr===false)$arr=array(array("false")); if($arr===null)$arr=array(array("null")); echo "<table border='1'>"; foreach($arr as $k1=>$v1) { //********************** // header if($k1==0) { echo "<tr>"; foreach($v1 as $k2=>$v2) { echo "<th>"; echo $k2; echo "</th>"; } echo "</tr>"; } //********************** //content echo "<tr>"; foreach($v1 as $k2=>$v2) { echo "<td>"; echo $v2; echo "</td>"; } echo "</tr>"; } echo "</table>"; } //SQL character escape (to prevent SQL injection)----It seems a bit redundant here, but it's not. public function quote($str){ $this->connect(); $s=$this->pdodb->quote($str); $this->pdodb=null;//Disconnect from the database server return $s; } public function getall($sql=null,$index='a'){ $a=$this->queryarr($sql,$index); return $a; } public function getrow($sql=null,$index='a'){ $a=$this->queryarr($sql,$index); return $a; } public function getone($sql=null,$index='n'){ $a=$this->queryarr($sql,$index); if($a===true)return $a; if($a===false)return $a; if($a==null)return null; return $a[0][0]; } //test public function test(){ /* $a=123; $b=$this->quote($a); echo "<br>asdg asdf $b"; */ //$sql="select session_content from session where session_id='$sess_id'"; //increase //$sql="replace into session values('abcd','cccc',unix_timestamp())"; //$sql="replace into session values('aaaa','cccc',unix_timestamp())"; //$sql="insert into session values('abcd11','cccc1',unix_timestamp())"; //$sql="select * from session where session_id='abcd'"; //$sql="select * from session where session_id='abcd222'"; //$sql="set a=1"; $sql="select * from session "; //$sql="select * from session where session_id='777'"; //$sql="select * from session"; //$sql=null; //$this->query($sql,0); $this->queryarr($sql); //$a=$this->res->fetchAll(PDO::FETCH_ASSOC);//Field subscript //$this->connect(); //echo "<pre>"; //var_dump($this->pdodb); //var_dump($this->res); //var_dump($this->arr); //var_dump($a); //echo "</pre>"; $this->tab($this->arr); } } /* $cs=array(//connect the array 'host' =>'127.0.0.1', 'port' =>'3306', 'username' =>'ddv1999', 'password' =>'1234abcd', 'dbname' =>'ddv1999', 'charset' =>'utf8', ); $op1=pdodb::getdb($cs); //echo "<pre>"; // var_dump ($ op1); //echo "</pre>"; $op1->test(); */