PHP a simple database connection and text caching comprehensive class

From , 5 Years ago, written in PHP, viewed 237 times.
URL https://pastebin.vip/view/3b5020bb
  1. class Db{
  2.     protected $_connect;
  3.     protected $_db = Array();
  4.     protected $_cache = Array();
  5.  
  6.     public function __construct($args){
  7.         list($this->_db,$this->_cache) = $args;
  8.     }
  9.     protected function connect($db){
  10.         $this->_connect = mysql_connect($db['hostname'],$db['username'],$db['password']);
  11.         mysql_set_charset('UTF8');
  12.         mysql_select_db($db['databasename'],$this->_connect);
  13.     }
  14.     /**
  15.      *作用:获取表中数据,并将所查询到的数据格式化返回,返回格式是数组形式!
  16.      *$sql:传入的待执行的SQL语句,必须而且只能是SELECT
  17.      *
  18.     */
  19.     public function fetch($sql){
  20.         $result = '';
  21.         if(isset($this->_cache['expire'])){
  22.             $name = md5(strtolower(str_replace(' ','',$sql)));
  23.             $dir = substr($name,0,2);
  24.             $dir = $this->_cache['dir'].'/'.$dir;
  25.             !is_dir($dir) && mkdir($dir,0777);
  26.             !is_dir($dir) && mkdir($dir,0777);
  27.             $this->_cache['path'] = $dir.'/'.$name;
  28.             if(is_file($this->_cache['path']) && $this->check_expire()){
  29.                 $result = $this->get();
  30.             }
  31.         }
  32.         if($result == ''){
  33.             $data = $this->exec($sql);
  34.             $result = Array();
  35.             while($result[] = mysql_fetch_array($data,MYSQL_ASSOC)){} //去除索引
  36.             mysql_free_result($data);
  37.             array_pop($result);
  38.             isset($this->_cache['expire']) && $this->write($result);
  39.         }
  40.         return $result;
  41.     }
  42.     /**
  43.      *作用:执行所有的SQL语句,但不包括SELECT!
  44.      *$sql:传入的待执行的SQL语句,不能为SELECT
  45.      *返回值:TRUE OR FALSE
  46.     */
  47.     public function exec($sql){
  48.         if($this->_connect === null) $this->connect($this->_db); //进行数据链接
  49.         if( $result = mysql_query($sql, $this->_connect) ){
  50.             return $result;
  51.         }else{
  52.             die("{$sql}<br />执行错误: " . mysql_error());
  53.         }
  54.     }
  55.     /**
  56.      *作用:执行数据库插入语句,只能是INSERT语句!
  57.      *$v:传入的待执行的条件,是数组格式table代表待执行插入的表,row是字段,value是待插入的值
  58.      *返回值:mysql_insert_id() OR FALSE
  59.     */
  60.     public function insert($table,$field,$ignore = 0){
  61.         $D = Array('field'=>'','val'=>'');
  62.         foreach($field AS $key => $v){
  63.             $D['field'] .= $key.',';
  64.             $D['val'] .= "'{$this->escape($v)}',";
  65.         }
  66.         $D['field'] = rtrim($D['field'],',');
  67.         $D['val'] = rtrim($D['val'],',');
  68.         $ignore = $ignore > 0 ? 'IGNORE' : '';
  69.         $sql = "INSERT {$ignore} INTO {$this->_db['perfix']}{$table}({$D['field']}) VALUES({$D['val']})";
  70.         if($this->exec($sql)){
  71.             $insert_id = mysql_insert_id();
  72.             return is_numeric($insert_id) ? $insert_id : TRUE;
  73.         }else{
  74.             return FALSE;
  75.         }
  76.     }
  77.     public function update($table,$field){
  78.         $D = Array('where'=>'','str'=>'');
  79.         $index = 0;
  80.         foreach($field AS $key => $v){
  81.             $index == 0 ? $D['where'] = "{$key} = '{$this->escape($v)}'" : $D['str'] .= "{$key} = '{$this->escape($v)}',";
  82.             $index++;
  83.         }
  84.         $D['str'] = rtrim($D['str'],',');
  85.         $sql = "UPDATE {$this->_db['perfix']}{$table} SET {$D['str']} WHERE {$D['where']}";
  86.         return $this->exec($sql);
  87.  
  88.     }
  89.     public function delete($table,$field){
  90.         $str = '';
  91.         foreach($field AS $key => $v){
  92.             $str = "{$key} = '{$v}'";
  93.         }
  94.         $sql = 'DELETE FROM '.$this->_db['perfix'].$table.' WHERE '.$str.' LIMIT 1';
  95.         return $this->exec($sql);
  96.     }
  97.     public function sum($table,$condition){
  98.         $totle = $this->fetch('SELECT COUNT(*) AS totle FROM '.$this->_db['perfix'].$table.' WHERE '.$condition);
  99.         return $totle[0]['totle'];
  100.     }
  101.     /**
  102.      *作用:对输入特殊字符进行过滤
  103.      *$v:待传入检测的参数
  104.      *返回值:检测完的参数
  105.      */
  106.     public function escape($v){
  107.         return mysql_real_escape_string($v);
  108.     }
  109.     /*
  110.      *作用:进行缓存判断
  111.      */
  112.     public function cache($name,$expire=100000000){
  113.         $this->_cache['expire'] = $expire;
  114.         return $this;
  115.     }
  116.     public function check_expire(){
  117.         return (filemtime($this->_cache['path']) + $this->_cache['expire']) > strtotime("now");
  118.     }
  119.  
  120.     public function write($data){
  121.         $f = fopen($this->_cache['path'], 'w');
  122.         if ($f) {
  123.             flock($f, LOCK_EX);
  124.             fseek($f, 0);
  125.             ftruncate($f, 0);
  126.             $tmp = fwrite($f, serialize($data));
  127.             if (!($tmp === false)) {
  128.                 $result = true;
  129.             }
  130.             fclose($f);
  131.         }
  132.         chmod($this->_cache['path'],0777);
  133.     }
  134.     public function get(){
  135.         $f = fopen($this->_cache['path'], 'r');
  136.         $data = fread($f,filesize($this->_cache['path']));
  137.         fclose($f);
  138.         return unserialize($data);
  139.     }
  140.     public function delete_dir($dir = ''){
  141.         $dir = empty($dir) ? $this->_cache['dir'] : $dir;
  142.         !is_dir($dir) && exit;
  143.         $d = opendir($dir);
  144.         $i = 0;
  145.         while(($file = readdir($d)) !== false){
  146.             $path = $dir.'/'.$file;
  147.             if($i > 1) is_file($path) ? unlink($path) : $this->delete_dir($path);
  148.             $i++;
  149.         }
  150.         closedir($d);
  151.         rmdir($dir);
  152.     }
  153.     public function __destruct(){
  154.         isset($this->_connect) && mysql_close($this->_connect);
  155.     }  
  156. }
  157. //该片段来自于http://yuncode.net
  158.  

Reply to "PHP a simple database connection and text caching comprehensive class"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website