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

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

WBOY
發布: 2016-07-23 08:54:56
原創
911 人瀏覽過
} }
[PHP]代碼
  1. class Db{
  2. protected $_connect;
  3. protected $_db = Array();
  4. protected $_cache = Arrayted $_db = Array();
  5. protected $_cache = Array();
  6. public function __construct($args){
  7. list($this->_db,$this->_cache) = $args;
  8. }
  9. protected function connect($db){
  10. }
  11. protected function connect($db){
  12. }
  13. protected function connect($db){
  14. }
  15. protected function connect($db){
  16. }
  17. protected function connect($db){
  18. }
  19. protected function connect($db){
  20. }
  21. protected function connect($db){
  22. }
  23. 生產商> $this->_connect = mysql_connect($db['主機名稱'],$db['使用者名稱'],$db['密碼']);
  24. mysql_set_charset('UTF8');
  25. mysql_select_db ($db['databasename'],$this->_connect);
  26. }
  27. /**
  28. *作用:獲取表中數據,並將所查詢到的數據格式化返回,返回格式是數組形式!
  29. *$sql:傳入的待執行的SQL語句,必須且只能是SELECT
  30. *
  31. */
  32. public function fetch($sql){
  33. $result = '' ;
  34. if(isset($this->_cache['expire'])){
  35. $name = md5(strtolower(str_replace(' ','',$sql)));
  36. $dir = substr($name,0,2);
  37. $dir = $this->_cache['dir'].'/'.$dir;
  38. !is_dir($dir) && mkdir ($dir, 0777);
  39. !is_dir($dir) && mkdir($dir,0777);
  40. $this->_cache['path'] = $dir.'/'.$name;
  41. if(is_file($this->_cache['path']) && $this->check_expire()){
  42. $result = $this->get();
  43. }
  44. }
  45. if($result == ''){
  46. $data = $this->exec($sql);
  47. $result = Array();
  48. while($result [] = mysql_fetch_array ($data,MYSQL_ASSOC)){} // 取出索引
  49. mysql_free_result($data);
  50. array_pop($result);
  51. isset($this->_cache['expire') ]) && $ this->write($result);
  52. }
  53. return $result;
  54. }
  55. /** *作用:執行所有的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語句!
  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);
}
} } } } } 🎜> 綜合類, php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板