- PDO Mode Connection Database class
<?PHP/** * @author Huang Gongyan * createtime 2018/5/28 0028 09:44*/classDb {//Privatization of database connection data, which can be used to specify a database connection (the basis for writing a framework) by getinstance incoming connection data Private $config= [ ' dbname ' = ' shunk ', ' username ' and ' root ', ' password ' + ' root ' ]; Private Static $instance=NULL; Private $conn=NULL; //privatization constructs a method to prevent external instantiation of this class, that is, to prevent the new Db () Private function__construct () {$this-Connect (); } //privatization of cloning methods to prevent external clones from replicating this class Private function__clone () {//todo:implement __clone () method. } //Guaranteed inheritance uniqueness, external access to this class only through this static method Public Static functiongetinstance () {if(! (Self::$instanceinstanceof Self)) { Self::$instance=NewSelf (); } returnSelf::$instance; } //How to connect a database in PDO mode Private functionConnect () {Try{ $dns= ' mysql:dbname= '.$this->config[' dbname '; Host=localhost;port=3306;charset=utf8 '; $this->conn =NewPDO ($dns,$this->config[' username '),$this->config[' Password ']); $this->conn->query (' SET NAMES UTF8 '); }Catch(pdoexception$e){ die(' Database connection failed '.$e-getMessage ()); } } //Query a record Public functionFetch$sql){ return $this->conn->query ($sql)->fetch (PDO::FETCH_ASSOC); } //querying more than one record Public functionFetchall ($sql){ return $this->conn->query ($sql)->fetchall (PDO::FETCH_ASSOC); } //Delete and modify the method, the number of rows affected successfully returned. Insert ID value returned successfully for insertion Public function exec($sql){ $num=$this->conn->exec($sql); if($num){ if(' 0 '! =$this->conn->Lastinsertid ()) { return $this->conn->Lastinsertid (); } return $num; }Else{ $error=$this->conn->Errinfo (); return' Operation failed '.$error[0]. ': '.$error[1]. ', '.$error[2]; } }}
2. caching php files
<?PHP/** * @author Huang Gongyan * createtime 2018/5/28 0028 09:06*/$fileName= '.. /runtime/filecache.php ';$time= 15;//determine if the cache file exists and the cache file is out of dateif(file_exists($fileName) && (Filemtime($fileName)+$time) >= Time()){ include(‘.. /runtime/filecache.php ');}Else{ Ob_start();//Open Memory Cache include(‘.. /db.php '); $db= Db::getinstance (); $sql= ' SELECT * from Sk_url '; $info=$db->fetchall ($sql); include' Index1.php '; $str=ob_get_contents();//get the cache content file_put_contents($fileName,$str);//Write Cache Ob_flush();//Close the memory cache}
3.HTML file
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Test</title></Head><Body> <Tablewidth= "All"Border= "1"style= "width:700px; margin:0 auto;"> <thead> <TR> <th>Id</th> <th>Url</th> <th>Short</th> <th>Status</th> <th>Create_time</th> </TR> </thead> <tbody> <?php foreach ($info as $v) {;?> <TR> <TD><?php echo $v [' id '];?></TD> <TD><?php echo $v [' url '];?></TD> <TD><?php echo $v [' short '];?></TD> <TD><?php echo $v [' status '];?></TD> <TD><?php echo $v [' create_time '];?></TD> </TR> <?php};?> </tbody> </Table></Body></HTML>
A simple example of this file cache is completed.
The file cache for PHP