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.

119 lines
3.2 KiB

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