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']);
}
/**
*作用:获取表中数据,并将所查询到的数据格式化返回,返回格式是数组形式!
*$sql:传入的待执行的SQL语句,必须而且只能是SELECT
*
*/
public function fetch($sql){
$result = '';
if(isset($this->_cache
['expire'])){
$dir = $this->_cache['dir'].'/'.$dir;
$this->_cache['path'] = $dir.'/'.$name;
if(is_file($this->_cache
['path']) && $this->check_expire()){
$result = $this->get();
}
}
if($result == ''){
$data = $this->exec($sql);
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); //进行数据链接
return $result;
}else{
}
}
/**
*作用:执行数据库插入语句,只能是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']})";
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){
}
/*
*作用:进行缓存判断
*/
public function cache($name,$expire=100000000){
$this->_cache['expire'] = $expire;
return $this;
}
public function check_expire(){
}
public function write($data){
$f = fopen($this->_cache
['path'], 'w');
if ($f) {
if (!($tmp === false)) {
$result = true;
}
}
chmod($this->_cache
['path'],0777);
}
public function get(){
$f = fopen($this->_cache
['path'], 'r');
}
public function delete_dir($dir = ''){
$dir = empty($dir) ?
$this->_cache
['dir'] : $dir;
$i = 0;
while(($file = readdir($d)) !== false){
$path = $dir.'/'.$file;
if($i > 1) is_file($path) ?
unlink($path) : $this->delete_dir($path);
$i++;
}
}
public function __destruct(){
}
}
//该片段来自于http://yuncode.net