A simple database connection and text cache synthesis class

Source: Internet
Author: User
Tags rtrim
There's nothing special about enclosing the database connection and the text cache in a class.
  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->_connect = mysql_connect ($db [' hostname '], $db [' username '], $db [' Password ']);
  10. Mysql_set_charset (' UTF8 ');
  11. mysql_select_db ($db [' DatabaseName '], $this->_connect);
  12. }
  13. /**
  14. * Function: Get the data in the table, and return the query to the data format, the return format is an array form!
  15. * $SQL: Incoming SQL statement to be executed, must and can only be select
  16. *
  17. */
  18. Public function Fetch ($SQL) {
  19. $result = ";
  20. if (isset ($this->_cache[' expire ')) {
  21. $name = MD5 (Strtolower (Str_replace (",", $sql)));
  22. $dir = substr ($name, 0,2);
  23. $dir = $this->_cache[' dir ']. ' /'. $dir;
  24. !is_dir ($dir) && mkdir ($dir, 0777);
  25. !is_dir ($dir) && mkdir ($dir, 0777);
  26. $this->_cache[' path ' = $dir. '/'. $name;
  27. if (Is_file ($this->_cache[' path ') && $this->check_expire ()) {
  28. $result = $this->get ();
  29. }
  30. }
  31. if ($result = = ") {
  32. $data = $this->exec ($sql);
  33. $result = Array ();
  34. while ($result [] = Mysql_fetch_array ($data, Mysql_assoc)) {}//Remove index
  35. Mysql_free_result ($data);
  36. Array_pop ($result);
  37. Isset ($this->_cache[' expire ') && $this->write ($result);
  38. }
  39. return $result;
  40. }
  41. /**
  42. * Function: Execute all SQL statements, but do not include select!
  43. * $SQL: Incoming SQL statement to execute, cannot be a select
  44. * Return value: TRUE OR FALSE
  45. */
  46. Public function exec ($sql) {
  47. if ($this->_connect = = = null) $this->connect ($this->_db); Link to data
  48. if ($result = mysql_query ($sql, $this->_connect)) {
  49. return $result;
  50. }else{
  51. Die ("{$sql}
    Execution error: ". Mysql_error ());
  52. }
  53. }
  54. /**
  55. * Function: Execute database INSERT statement, only INSERT statement!
  56. * $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
  57. * Return value: 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. * Function: Filter the input special characters
  101. * $v: Parameters for incoming detection
  102. * Return value: Detected parameters
  103. */
  104. Public function Escape ($v) {
  105. Return mysql_real_escape_string ($v);
  106. }
  107. /*
  108. * Function: Make cache judgment
  109. */
  110. Public Function cache ($name, $expire =100000000) {
  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. Public Function __destruct () {
  151. Isset ($this->_connect) && mysql_close ($this->_connect);
  152. }
  153. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.