make a note here
Several knowledge points of PHP class
self::$static property name
$this->General property name or method name
Four main points of singleton class (three private and one public)
1 Prohibition of constructing objects---prohibiting new() private __construct(){} private empty definition
2 Prohibit cloning objects -- clone private __clone(){} private empty definition
3 Define singleton object properties private static $object name private static empty definition
4 Define the entry method public static function method name () public static
face-to-face
<?php class mysqli_object_db{ //********************************* //public protected private private $mysqli;//Connection object private $result;//result object public $arr;//Result array //Define connection parameters public $cs=array( 'host' =>'127.0.0.1', 'port' =>'3306', 'username' =>'root', 'password' =>'', 'dbname' =>'mysql', 'charset' =>'utf8', ); //********************************* //Define singleton (three private and one public) //private static object private static $db; //Private construction: prohibit construction---new() private function __construct(){} //Private clone: Prohibit clone private function __clone(){} //Singleton object entry public static function getdb($arr=null){ //If the object does not exist, create the object if(!isset(self::$db)){self::$db=new self();} //if there are connection parameters 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 member method private function connect(){ //$this->db = @ new mysqli( $this->mysqli = @ new mysqli( $this->cs["host"].":".$this->cs["port"], $this->cs["username"], $this->cs["password"] ); /* check connection */ if (mysqli_connect_errno()) { echo "<br>*************************************"; echo "<br>Connection failed!!!"; echo "<br>Error code:".mysqli_connect_errno(); echo "<br>Error message:".mysqli_connect_error(); echo "<br>*************************************"; exit("<br>stop!!!"); } } //set encoding private function charset($char="utf8"){ $this->mysqli->query("set names $char"); } //open database private function usedb($dbname="mysql"){ $this->mysqli->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->result = $this->mysqli->query($sql); if($erro!==null){ if(!$this->result){ echo "<br>****************************"; echo "<br>Failed to execute"; echo "<br>命令:".$sql; echo "<br>Error code:".$this->mysqli->errno; echo "<br>Error message:".$this->mysqli->error; echo "<br>****************************"; die("<br>STOP!!!"); } } return $this->result; } // 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;} //$r=$this->query($sql,0);//Add the second parameter to open the error $res=$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($res===true){ $this->arr=true; }else{ if($res===false){ $this->arr=false; }else{ //Get all data three types of subscripts switch ($index) { case 'a': $this->arr=$res->fetch_all(MYSQLI_ASSOC);//Field subscript break; case 'n': $this->arr=$res->fetch_all(MYSQLI_NUM);//Numerical subscript break; case 'b': $this->arr=$res->fetch_all(MYSQLI_BOTH);//Double subscript break; } $res->free();//Release the result object } } $this->mysqli->close();//Close the connection object $this->mysqli=null;//Reset $this->result=null;//Reset return $this->arr; } //********************** //Pure output data to table table public function tab($arr) { if(gettype($arr)!='array'){ if($arr===true){ $arr=array(array('true')); }else{ $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 public function escape($str){ $this->connect(); $s=$this->mysqli->escape_string($str); $this->mysqli->close();//Disconnect from the database server $this->mysqli=null; return $s; } // test member method public function test(){ /* $a=123; $b=$this->escape($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'"; //$this->queryarr($sql); //$this->connect(); //$this->ready(); //echo "<br>".$this->cs['charset']; //echo "<pre>"; //var_dump($this->arr); //echo "</pre>"; //$this->tab($this->arr); } } $cs=array( 'host' =>'127.0.0.1', 'port' =>'3306', 'username' =>'xxxx', 'password' =>'1234abcd', 'dbname' =>'xxxx', 'charset' =>'utf8', ); $op1=mysqli_object_db::getdb($cs); echo "<pre>"; // var_dump ($ op1); echo "</pre>"; $op1->test();
process oriented
<?php class mysqli_process_db{ //********************************* //public protected private private $link;//Connection object private $result;//result object public $arr;//Result array //Define connection parameters public $cs=array( 'host' =>'127.0.0.1', 'port' =>'3306', 'username' =>'root', 'password' =>'', 'dbname' =>'mysql', 'charset' =>'utf8', ); //********************************* //Define singleton (three private and one public) //private static object private static $db; //Private construction: prohibit construction---new() private function __construct(){} //Private clone: Prohibit clone private function __clone(){} //Singleton object entry public static function getdb($arr=null){ //If the object does not exist, create the object if(!isset(self::$db)){self::$db=new self();} //if there are connection parameters 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 member method private function connect(){ //$this->db = @ new mysql( $this->link = @mysqli_connect( $this->cs["host"].":".$this->cs["port"], $this->cs["username"], $this->cs["password"] ); /* check connection */ if (mysqli_connect_errno()) { echo "<br>*************************************"; echo "<br>Connection failed!!!"; echo "<br>Error code:".mysqli_connect_errno(); echo "<br>Error message:".mysqli_connect_error(); echo "<br>*************************************"; exit("<br>stop!!!"); } } //set encoding private function charset($char="utf8"){ mysqli_query($this->link,"set names $char"); } //open database private function usedb($dbname="mysql"){ mysqli_query($this->link,"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->result =mysqli_query($this->link,$sql); if($erro!==null){ if(!$this->result){ echo "<br>****************************"; echo "<br>Failed to execute"; echo "<br>命令:".$sql; echo "<br>Error code:".mysqli_errno($this->link); echo "<br>Error information:".mysqli_error($this->link); echo "<br>****************************"; die("<br>STOP!!!"); } } return $this->result; } // 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;} //$res=$this->query($sql,0);//Add the second parameter to open the error $res=$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 the local data if($res===true){ $this->arr=true; }else{ if($res===false){ $this->arr=false; }else{ // get the entire array //MYSQLI_ASSOC/Field subscript, MYSQLI_NUM/Number subscript, MYSQLI_BOTH/Field and number double subscript switch ($index) { case 'a'://Field subscript $this->arr=mysqli_fetch_all($res,MYSQLI_ASSOC); break; case 'n'://numeric subscript $this->arr=mysqli_fetch_all($res,MYSQLI_NUM); break; case 'b': $this->arr=mysqli_fetch_all($res,MYSQLI_BOTH); break; } mysqli_free_result ($res);//Release the result resource of the database server } } mysqli_close ($this->link);//Disconnect the database server $this->link=null;//Reset $this->result=null;//Reset return $this->arr; } //********************** //Pure output data to table table public function tab($arr) { if(gettype($arr)!='array'){ if($arr===true){ $arr=array(array('true')); }else{ $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>"; } public function escape($str){ $this->connect(); $s=mysqli_escape_string($this->link,$str); mysqli_close ($this->link);//Disconnect the database server $this->link=null; return $s; } // test member method public function test(){ /* $a=123; $this->connect(); $b=mysqli_escape_string($this->link,$a); mysqli_close ($this->link);//Disconnect the database server $this->link=null; $c="asdf asd $b"; echo $c; */ //$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 session_content from session where session_id='abcd'"; $this->queryarr($sql); //$this->connect(); //$this->ready(); //echo "<br>".$this->cs['charset']; //echo "<pre>"; //var_dump($this->arr); //echo "</pre>"; $this->tab($this->arr); } }