您好,欢迎访问一九零五行业门户网

一个简单的php数据库连接和文本缓存综合类

[php]代码 class db{ protected $_connect; protected $_db = array(); protected $_cache = array(); public function __construct($args){ list($this->_db,$this->_cache) = $args; } protected function connect($db){ $this->_connect = mysql_connect($db['hostname'],$db['username'],$db['password']); mysql_set_charset('utf8'); mysql_select_db($db['databasename'],$this->_connect); } /** *作用:获取表中数据,并将所查询到的数据格式化返回,返回格式是数组形式! *$sql:传入的待执行的sql语句,必须而且只能是select * */ public function fetch($sql){ $result = ''; if(isset($this->_cache['expire'])){ $name = md5(strtolower(str_replace(' ','',$sql))); $dir = substr($name,0,2); $dir = $this->_cache['dir'].'/'.$dir; !is_dir($dir) && mkdir($dir,0777); !is_dir($dir) && mkdir($dir,0777); $this->_cache['path'] = $dir.'/'.$name; if(is_file($this->_cache['path']) && $this->check_expire()){ $result = $this->get(); } } if($result == ''){ $data = $this->exec($sql); $result = array(); while($result[] = mysql_fetch_array($data,mysql_assoc)){} //去除索引 mysql_free_result($data); array_pop($result); isset($this->_cache['expire']) && $this->write($result); } return $result; } /** *作用:执行所有的sql语句,但不包括select! *$sql:传入的待执行的sql语句,不能为select *返回值:true or false */ public function exec($sql){ if($this->_connect === null) $this->connect($this->_db); //进行数据链接 if( $result = mysql_query($sql, $this->_connect) ){ return $result; }else{ die({$sql}
执行错误: . mysql_error()); } } /** *作用:执行数据库插入语句,只能是insert语句! *$v:传入的待执行的条件,是数组格式table代表待执行插入的表,row是字段,value是待插入的值 *返回值:mysql_insert_id() or false */ public function insert($table,$field,$ignore = 0){ $d = array('field'=>'','val'=>''); foreach($field as $key => $v){ $d['field'] .= $key.','; $d['val'] .= '{$this->escape($v)}',; } $d['field'] = rtrim($d['field'],','); $d['val'] = rtrim($d['val'],','); $ignore = $ignore > 0 ? 'ignore' : ''; $sql = insert {$ignore} into {$this->_db['perfix']}{$table}({$d['field']}) values({$d['val']}); if($this->exec($sql)){ $insert_id = mysql_insert_id(); return is_numeric($insert_id) ? $insert_id : true; }else{ return false; } } public function update($table,$field){ $d = array('where'=>'','str'=>''); $index = 0; foreach($field as $key => $v){ $index == 0 ? $d['where'] = {$key} = '{$this->escape($v)}' : $d['str'] .= {$key} = '{$this->escape($v)}',; $index++; } $d['str'] = rtrim($d['str'],','); $sql = update {$this->_db['perfix']}{$table} set {$d['str']} where {$d['where']}; return $this->exec($sql); } public function delete($table,$field){ $str = ''; foreach($field as $key => $v){ $str = {$key} = '{$v}'; } $sql = 'delete from '.$this->_db['perfix'].$table.' where '.$str.' limit 1'; return $this->exec($sql); } public function sum($table,$condition){ $totle = $this->fetch('select count(*) as totle from '.$this->_db['perfix'].$table.' where '.$condition); return $totle[0]['totle']; } /** *作用:对输入特殊字符进行过滤 *$v:待传入检测的参数 *返回值:检测完的参数 */ public function escape($v){ return mysql_real_escape_string($v); } /* *作用:进行缓存判断 */ public function cache($name,$expire=100000000){ $this->_cache['expire'] = $expire; return $this; } public function check_expire(){ return (filemtime($this->_cache['path']) + $this->_cache['expire']) > strtotime(now); } public function write($data){ $f = fopen($this->_cache['path'], 'w'); if ($f) { flock($f, lock_ex); fseek($f, 0); ftruncate($f, 0); $tmp = fwrite($f, serialize($data)); if (!($tmp === false)) { $result = true; } fclose($f); } chmod($this->_cache['path'],0777); } public function get(){ $f = fopen($this->_cache['path'], 'r'); $data = fread($f,filesize($this->_cache['path'])); fclose($f); return unserialize($data); } public function delete_dir($dir = ''){ $dir = empty($dir) ? $this->_cache['dir'] : $dir; !is_dir($dir) && exit; $d = opendir($dir); $i = 0; while(($file = readdir($d)) !== false){ $path = $dir.'/'.$file; if($i > 1) is_file($path) ? unlink($path) : $this->delete_dir($path); $i++; } closedir($d); rmdir($dir); } public function __destruct(){ isset($this->_connect) && mysql_close($this->_connect); } }
复制代码
综合类, php
其它类似信息

推荐信息