There's nothing special about enclosing the database connection and the text cache in a class.
- 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);
- }
- /**
- * Function: Get the data in the table, and return the query to the data format, the return format is an array form!
- * $SQL: Incoming SQL statement to be executed, must and can only be 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)) {}//Remove index
- Mysql_free_result ($data);
- Array_pop ($result);
- Isset ($this->_cache[' expire ') && $this->write ($result);
- }
- return $result;
- }
- /**
- * Function: Execute all SQL statements, but do not include select!
- * $SQL: Incoming SQL statement to execute, cannot be a select
- * Return value: TRUE OR FALSE
- */
- Public function exec ($sql) {
- if ($this->_connect = = = null) $this->connect ($this->_db); Link to data
- if ($result = mysql_query ($sql, $this->_connect)) {
- return $result;
- }else{
- Die ("{$sql}
Execution error: ". Mysql_error ());
- }
- }
- /**
- * Function: Execute database INSERT statement, only INSERT statement!
- * $v: The incoming pending condition, is the array format table represents the tables to be inserted, row is the field, value is the values to be inserted
- * Return 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 '];
- }
- /**
- * Function: Filter the input special characters
- * $v: Parameters for incoming detection
- * Return value: Detected parameters
- */
- Public function Escape ($v) {
- Return mysql_real_escape_string ($v);
- }
- /*
- * Function: Make cache judgment
- */
- 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);
- }
- }
Copy Code |