DBHost=$Host; $this->DBName=$Name; $this->DBUser=$User; $this->DBPassword=$Password; $this->DBLink = new mysqli( $this->DBHost, $this->DBUser, $this->DBPassword, $this->DBName ); if( $this->DBLink->connect_error ){ if( $DBCREATE ){ $this->DBLink = new mysqli( $this->DBHost, $this->DBUser, $this->DBPassword ); if( $this->DBLink->connect_error ){ $this->isConnected=false; //echo 'Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error; } else { if (!$this->DBLink->set_charset("utf8")) { printf("Error loading character set utf8: %s\n", $mysqli->error); } else { //printf("Current character set: %s\n", $mysqli->character_set_name()); }; $QS = "CREATE DATABASE {$Name}"; if( mysqli_query( $this->DBLink, $QS ) === false ) { echo " Error during query [$QS]:".mysqli_error($this->DBLink); $this->isConnected=false; } else { $this->isConnected=true; }; }; } else { $this->isConnected=false; //echo 'Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error; }; } else { //mysql_query("SET NAMES 'UTF8'",$this->DBLink); if (!$this->DBLink->set_charset("utf8")) { printf("Error loading character set utf8: %s\n", $mysqli->error); } else { //printf("Current character set: %s\n", $mysqli->character_set_name()); }; $this->isConnected=true; }; } 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 = mysqli_query( $this->DBLink, $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink)); $RetVal=array(); while($array=mysqli_fetch_assoc($result)) $RetVal[]=$array; $FData=serialize($RetVal); $FD=fopen($Prefetch,'w'); fwrite($FD,$FData); fclose($FD); return $RetVal; } } else { $result = mysqli_query( $this->DBLink, $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink)); $RetVal=array(); while($array=mysqli_fetch_assoc($result)) $RetVal[]=$array; $FData = serialize($RetVal); $FD=fopen($Prefetch,'w'); fwrite($FD,$FData); fclose($FD); return $RetVal; } } else { $result = mysqli_query( $this->DBLink, $QS ) or die (" Error during query [$QS]:".mysqli_error($this->DBLink)); $RetVal=array(); if ($result !== false && is_a( $result, 'mysqli_result' ) ){ while($array=mysqli_fetch_assoc($result)) $RetVal[]=$array; }; return $RetVal; }; } else { echo "No Connection to DB
"; }; } function getLastInsertID(){ if($this->isConnected){ return $this->DBLink->insert_id; }; return -1; } function __destruct(){ if( $this->isConnected ){ $this->DBLink->close(); }; } } ?>