首頁 >後端開發 >php教程 >一個簡單的資料庫連接和文字快取綜合類

一個簡單的資料庫連接和文字快取綜合類

WBOY
WBOY原創
2016-07-25 08:49:36894瀏覽
} }
沒什麼特色,就是把資料庫連線和文字快取封裝在一個類別裡面了
  1. class Db{
  2. protected $_connect;
  3. protected $_db = Array();
  4. protected $_cache = Array();
  5. public function __construct($args){
  6. list($this->_db,$this->_cache) = $args;
  7. }
  8. protected function connect($db){
  9. $this-> 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}
    執行錯誤: " . mysql_error());
  53. }
  54. }
  55. /** *作用:執行資料庫插入語句,只能是INSERT語句!
  56. *$v:傳入的待執行的條件,是數組格式table代表待執行插入的表,row是字段,value是待插入的值
  57. *返回值:mysql_insert_id() OR FALSE
  58. */
  59. public function insert($table,$field,$ignore = 0){
  60. $D = Array('field'=>'','val'=>'');
  61. foreach($field AS $key => $v){
  62. $D['field'] .= $key.',';
  63. $D['val'] .= "'{$this- >escape($v)}',";
  64. }
  65. $D['field'] = rtrim($D['field'],',');
  66. $D['val' ] = rtrim($D['val'],',');
  67. $ignore = $ignore > 0 ? 'IGNORE' : '';
  68. $sql = "INSERT {$ignore} INTO {$ this->_db['perfix']}{$table}({$D['field']}) VALUES({$D['val']})";
  69. if($this->exec( $sql)){
  70. $insert_id = mysql_insert_id();
  71. return is_numeric($insert_id) ? $insert_id : TRUE;
  72. }else{
  73. return FALSE
  74. }
  75. }
  76. public function update($table,$field){
  77. $D = Array('where'=>'','str'=>'');
  78. $index = 0;
  79. foreach($field AS $key => $v){
  80. $index == 0 ? $D['where'] = "{$key} = '{$this->escape($v)}'" : $D['str'] .= "{$key} = '{$this->escape($v)}',";
  81. $index ;
  82. }
  83. $D['str '] = rtrim($D['str'],',');
  84. $sql = "UPDATE {$this->_db['perfix']}{$table} SET {$D['str' ]} WHERE {$D['where']}";
  85. return $this->exec($sql);
  86. }
  87. public function delete($table,$field){
  88. $str = '';
  89. foreach($field AS $key => $v){
  90. $str = "{$key} = '{$v}'";
  91. }
  92. $sql = 'DELETE FROM '.$this->_db['perfix'].$table.' WHERE '.$str.' LIMIT 1';
  93. return $this->exec($sql);
  94. }
  95. public function sum($table,$condition){
  96. $totle = $this->fetch('SELECT COUNT(*) AS totle FROM '.$this->_db['perfix'] .$table.' WHERE '.$condition);
  97. return $totle[0]['totle'];
  98. }
  99. /**
  100. *作用:將輸入特殊字元過濾
  101. *$v:待傳入偵測的參數
  102. *傳回值:偵測完的參數
  103. */
  104. public function escape($ v){
  105. return mysql_real_escape_string($v);
  106. }
  107. /*
  108. *作用:進行快取判斷
  109. */
  110. public function cache($name,$expire=1000000000 ){
  111. $this->_cache['expire'] = $expire;
  112. return $this;
  113. }
  114. public function check_expire(){
  115. return (filemtime($this-> _cache['path']) $this->_cache['expire']) > strtotime("now");
  116. }
  117. public function write($data){
  118. $f = fopen($this->_cache['path'], 'w');
  119. if ($f) {
  120. flock($f, LOCK_EX);
  121. fseek($f, 0);
  122. ftruncate($f, 0);
  123. $tmp = fwrite($f, serialize($data));
  124. if (!($tmp === false)) {
  125. $result = true;
  126. }
  127. fclose($f);
  128. }
  129. chmod($this->_cache['path'],0777);
  130. }
  131. public function get() {
  132. $f = fopen($this->_cache['path'], 'r');
  133. $data = fread($f,filesize($this->_cache['path'])) ;
  134. fclose($f);
  135. return unserialize($data);
  136. }
  137. public function delete_dir($dir = ''){
  138. $dir = empty($dir) ? $this->_cache['dir'] : $dir;
  139. !is_dir($dir) && exit;
  140. $d = opendir($dir);
  141. $i = 0;
  142. while (($file = readdir($d)) !== false){
  143. $path = $dir.'/'.$file;
  144. if($i > 1) is_file($path) ? unlink ($path) : $this->delete_dir($path);
  145. $i ;
  146. }
  147. closedir($d);
  148. rmdir($dir);
  149. }
  150. }
  151. }
  152. }
}
} } }
}
} }
}
}
}
}
public function __destruct(){
isset($this->_connect) && mysql_close($this->_connect); } } } } } } 🎜>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn