DBHost=$Host; $this->DBName=$Name; $this->DBUser=$User; $this->DBPassword=$Password; if ($this->DBLink = mysql_connect($this->DBHost, $this->DBUser, $this->DBPassword)) { //mysql_query("SET NAMES 'UTF8'",$this->DBLink); mysql_set_charset('utf8', $this->DBLink ); $this->isConnected=true; } else { $this->isConnected=false; //echo "Error During Connection to Database!!
"; } } function CleanUpCache(){ if ($DD=@opendir($this->CachePath)){ while($FN=readdir($DD)){ $CF=$this->CachePath.'/'.$FN; if (is_file($CF)){ if ( strstr($FN,'dbchache_') && (time()-filemtime($CF))>360){ @unlink($CF); }; }; }; closedir($DD); } else $this->isCached=false; } function ResetCache(){ if ($DD=@opendir($this->CachePath)){ while($FN=readdir($DD)){ $CF=$this->CachePath.'/'.$FN; if (is_file($CF)){ if ( strstr($FN,'dbchache_')){ @unlink($CF); }; }; }; closedir($DD); } else $this->isCached=false; } function SetCacheDir($Path){ $this->CachePath = $Path; $this->isCached = is_dir($this->CachePath) && is_writable($this->CachePath); if ($this->isCached) $this->CleanUpCache(); } function escape( $Str ){ if($this->isConnected){ return $this->DBLink->real_escape_string( $Str ); }; return ''; } function Query($QS,$ChacheIfAveable=true){ if($this->isConnected){ if ($this->isCached && $ChacheIfAveable){ $QMD5=md5($QS); $Prefetch=$this->CachePath.'/dbchache_'.$QMD5; if ( is_file($Prefetch)){ if ( (time()- filemtime($Prefetch))<360){ $FData=file_get_contents($Prefetch); return unserialize($FData); } else { $result = mysql_db_query($this->DBName,$QS,$this->DBLink) or die (" Error during query [$QS]:".mysql_error($this->DBLink)); $RetVal=array(); while($array=mysql_fetch_assoc($result)) $RetVal[]=$array; $FData=serialize($RetVal); $FD=fopen($Prefetch,'w'); fwrite($FD,$FData); fclose($FD); return $RetVal; } } else { $result = mysql_db_query($this->DBName,$QS,$this->DBLink) or die (" Error during query [$QS]:".mysql_error($this->DBLink)); $RetVal=array(); while($array=mysql_fetch_assoc($result)) $RetVal[]=$array; $FData = serialize($RetVal); $FD=fopen($Prefetch,'w'); fwrite($FD,$FData); fclose($FD); return $RetVal; } } else { $result = mysql_db_query($this->DBName,$QS,$this->DBLink) or die (" Error during query [$QS]:".mysql_error($this->DBLink)); $RetVal=array(); if (is_resource($result)){ while($array=mysql_fetch_assoc($result)) $RetVal[]=$array; }; return $RetVal; }; } else { echo "No Connection to DB
"; } } function getLastInsertID(){ if($this->isConnected){ return mysql_insert_id( $this->DBLink ); }; return -1; } function __destruct(){ mysql_close($this->DBLink); } } ?>