You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. class MySQL_DB{
  3. var $DBHost;
  4. var $DBName;
  5. var $DBUser;
  6. var $DBPassword;
  7. var $DBLink;
  8. var $CachePath;
  9. var $isConnected=false;
  10. var $isCached=false;
  11. function MySQL_DB( $Host,$Name,$User,$Password){
  12. $this->DBHost=$Host;
  13. $this->DBName=$Name;
  14. $this->DBUser=$User;
  15. $this->DBPassword=$Password;
  16. if ($this->DBLink = mysql_connect($this->DBHost, $this->DBUser, $this->DBPassword)) {
  17. //mysql_query("SET NAMES 'UTF8'",$this->DBLink);
  18. mysql_set_charset('utf8', $this->DBLink );
  19. $this->isConnected=true;
  20. } else {
  21. $this->isConnected=false;
  22. //echo "Error During Connection to Database!!<br>";
  23. }
  24. }
  25. function CleanUpCache(){
  26. if ($DD=@opendir($this->CachePath)){
  27. while($FN=readdir($DD)){
  28. $CF=$this->CachePath.'/'.$FN;
  29. if (is_file($CF)){
  30. if ( strstr($FN,'dbchache_') && (time()-filemtime($CF))>360){
  31. @unlink($CF);
  32. };
  33. };
  34. };
  35. closedir($DD);
  36. } else $this->isCached=false;
  37. }
  38. function ResetCache(){
  39. if ($DD=@opendir($this->CachePath)){
  40. while($FN=readdir($DD)){
  41. $CF=$this->CachePath.'/'.$FN;
  42. if (is_file($CF)){
  43. if ( strstr($FN,'dbchache_')){
  44. @unlink($CF);
  45. };
  46. };
  47. };
  48. closedir($DD);
  49. } else $this->isCached=false;
  50. }
  51. function SetCacheDir($Path){
  52. $this->CachePath = $Path;
  53. $this->isCached = is_dir($this->CachePath) && is_writable($this->CachePath);
  54. if ($this->isCached) $this->CleanUpCache();
  55. }
  56. function escape( $Str ){
  57. if($this->isConnected){
  58. return $this->DBLink->real_escape_string( $Str );
  59. };
  60. return '';
  61. }
  62. function Query($QS,$ChacheIfAveable=true){
  63. if($this->isConnected){
  64. if ($this->isCached && $ChacheIfAveable){
  65. $QMD5=md5($QS);
  66. $Prefetch=$this->CachePath.'/dbchache_'.$QMD5;
  67. if ( is_file($Prefetch)){
  68. if ( (time()- filemtime($Prefetch))<360){
  69. $FData=file_get_contents($Prefetch);
  70. return unserialize($FData);
  71. } else {
  72. $result = mysql_db_query($this->DBName,$QS,$this->DBLink) or die (" Error during query [$QS]:".mysql_error($this->DBLink));
  73. $RetVal=array();
  74. while($array=mysql_fetch_assoc($result)) $RetVal[]=$array;
  75. $FData=serialize($RetVal);
  76. $FD=fopen($Prefetch,'w');
  77. fwrite($FD,$FData);
  78. fclose($FD);
  79. return $RetVal;
  80. }
  81. } else {
  82. $result = mysql_db_query($this->DBName,$QS,$this->DBLink) or die (" Error during query [$QS]:".mysql_error($this->DBLink));
  83. $RetVal=array();
  84. while($array=mysql_fetch_assoc($result)) $RetVal[]=$array;
  85. $FData = serialize($RetVal);
  86. $FD=fopen($Prefetch,'w');
  87. fwrite($FD,$FData);
  88. fclose($FD);
  89. return $RetVal;
  90. }
  91. } else {
  92. $result = mysql_db_query($this->DBName,$QS,$this->DBLink) or die (" Error during query [$QS]:".mysql_error($this->DBLink));
  93. $RetVal=array();
  94. if (is_resource($result)){
  95. while($array=mysql_fetch_assoc($result)) $RetVal[]=$array;
  96. };
  97. return $RetVal;
  98. };
  99. } else {
  100. echo "No Connection to DB<br>";
  101. }
  102. }
  103. function getLastInsertID(){
  104. if($this->isConnected){
  105. return mysql_insert_id( $this->DBLink );
  106. };
  107. return -1;
  108. }
  109. function __destruct(){
  110. mysql_close($this->DBLink);
  111. }
  112. }
  113. ?>