Host=$Host; else $this->sHost="localhost"; if(isset($Username)) $this->Username=$Username; if(isset($Password)) $this->Password=$Password; if($DatabaseName!="") $this->DatabaseName=$DatabaseName; } /** * Destructor for the class. * @return void */ function __destruct() { try { if($this->objConnection) $this->objConnection->Close(); } catch(Exception $e){} } /** * Connects to the database. Returns true on success, or false on failure. * @return void * @access private */ private function Connect($bRedirect=false) { try { //make sure to release any previous connection. unset($this->objConnection); //create a new mysqli class and connect. $objConnection=new mysqli($this->Host,$this->Username,$this->Password,$this->DatabaseName); //check for connection error if($objConnection->connect_error) { throw new Exception("MySQL Connection Failure: ".$objConnection->connect_error); } //check for connection error prior to PHP 5.2.9 if (mysqli_connect_error()) { throw new Exception("MySQL Connection Failure: ".mysqli_connect_error()); } //no error, so return the object and store a reference. $this->objConnection=$objConnection; return true; } catch(Exception $e) { //log the error $this->LogError("MySQL Connection Failure: ".$e->getMessage()); return false; } } /** * Escapes the value to be placed in a field. This helps prevent problems with quotes, and mitigates SQL injection attacks. * @param string The value to escape. * @return string */ public function Escape($Value) { if($this->TestConnection()) { return($this->objConnection->real_escape_string($Value)); } else return(false); } /** * Returns the number of rows affected by the previous query * @return int */ public function GetAffectedRows() { if(!$this->objConnection) return 0; return($this->objConnection->affected_rows); } /** * Gets the text of the last error, if any. * @return string */ public function GetLastError() { if(!$this->objConnection) return ""; return($this->objConnection->error); } /** * Inserts a record into the database. Returns the insert auto-incremented ID on success, false on failure. Unless UpdateTimestamps is set to false, fields will automatically be added for Modified and Created. * @param string The name of the table. * @param array An array of the fields/values to insert. Each element key is the column name for the table, and the value is the value to insert. * @return int */ public function Insert($Table,$Fields) { try { if(!is_array($Fields)) throw new Exception("Attempted to insert with no fields."); if(true===$this->TestConnection()) { foreach($Fields as $sKey => $sValue) { $aKeys[]=$sKey; if(substr_count($sValue,"password(")>0) $aValues[]=$sValue; //if we are using the password function, don't escape or add quotes. elseif(strtolower($sValue)=="null") $aValues[]="NULL"; //if we are setting a field to NULL, don't escape or add quotes. else $aValues[]='"'.$this->Escape($sValue).'"'; //escape and add quotes } $sQuery='INSERT INTO '.$Table.' ('.implode(",",$aKeys); if($this->UpdateTimestamps) $sQuery.=',Modified,Created'; $sQuery.=') VALUES ('.implode(',',$aValues); if($this->UpdateTimestamps) $sQuery.=',now(),now()'; $sQuery.=')'; $this->LastQuery=$sQuery; $bResult=$this->objConnection->query($sQuery); if(true==$bResult) return($this->objConnection->insert_id); else throw new Exception("Insert Failed: ".$sQuery." Error: ".$this->objConnection->error); } else return(false); } catch(Exception $e) { $this->LogError($e->getMessage()); return false; } } /** * Runs a raw query on the database that does not expect any return. Returns the result of that query, which may be a boolean for failure or an object for success of some queries. * @param string The query to run. * @return mixed */ public function Query($Query) { try { if($this->TestConnection()) { $this->LastQuery=$Query; $oResult=$this->objConnection->query($Query); if(false==$oResult) throw new Exception("Query failed: $query Error: ".$objConnection->error); else return $oResult; } else throw new Exception("Connection Failed."); } catch(Exception $e) { $this->LogError($e->getMessage()); return false; } } /** * Performs an Update query on the database. Returns true on success, false on failure. * @param string The name of the table. * @param array An associative array of the fields to update. Each element's key is the column name in the table, and the value of the element is the value to update. * @param boolean Whether or not to use delayed replace. Default false. * @return bool */ public function Replace($Table,$Fields,$Delayed=false) { try { if(!is_array($Fields)) throw new Exception("Attempted to update with no fields selected"); if($this->TestConnection()) { foreach($Fields as $sKey => $sValue) { $aKeys[]=$sKey; if(substr_count($sValue,"password(")>0) $aValues[]=$sValue; //if we are using the password function, don't escape or add quotes. elseif(strtolower($sValue)=="null") $aValues[]="NULL"; //if we are setting a field to NULL, don't escape or add quotes. else $aValues[]='"'.$this->Escape($sValue).'"'; //escape and add quotes } $sQuery='REPLACE'.(true===$Delayed?" DELAYED":"").' INTO '.$Table.' ('.implode(",",$aKeys); if(true===$this->UpdateTimestamps) $sQuery.=',Modified,Created'; $sQuery.=') VALUES ('.implode(',',$aValues); if(true===$this->UpdateTimestamps) $sQuery.=',now(),now()'; $sQuery.=')'; $this->LastQuery=$sQuery; $bResult=$this->objConnection->query($sQuery); if(true==$bResult) return true; else throw new Exception("Replace Failed: ".$sQuery." Error: ".$this->objConnection->error); } else throw new Exception("Failed to conenct to database."); } catch(Exception $e) { $this->LogError($e->getMessage()); return false; } } /** * Runs a Select query on the database. Returns an associative array of rows on success, false on failure. This will also loop through all of the results and clean the data (stripslashes). * @param string The query to run. * @return mixed */ public function Select($Query) { try { if($this->TestConnection()) { $this->LastQuery=$Query; $oResult=$this->objConnection->query($Query); if(false===$oResult) throw new Exception("Select failed: $Query Error: ".$this->objConnection->error); while($aRow=$oResult->fetch_assoc()) { foreach($aRow as $sKey => $sValue) $aRow[$sKey]=stripslashes($sValue); $aReturn[]=$aRow; } } else throw new Exception("Failed to connect to database."); return($aReturn); } catch(Exception $e) { $this->LogError($e->getMessage()); return false; } } /** * Tests to make sure the connection to the database is open, and if not, attempts to re-open the connection. * @return bool */ public function TestConnection() { try { if(!$this->objConnection) { if(false===$this->Connect()) throw new Exception("Failed to connect in TestConnection."); } if(false===$this->objConnection->ping()) throw new Exception("Failed to ping connection in TestConnection."); return(true); } catch(Exception $e) { $this->LogError($e->getMessage()); return false; } } /** * Performs an Update query on the database. Returns success or failure. * @param string The name of the table. * @param array An associative array of the fields to update. Each element's key is the column name in the table, and the value of the element is the value to update. * @param string The WHERE clause of the query. * @param int Optional. The limit to the number of rows that should be affected. * @return bool */ public function Update($Table,$Fields,$Condition,$Limit=0) { try { if(!is_array($Fields)) throw new Exception("Tried to update with an empty field list."); if(true===$this->TestConnection()) { foreach($Fields as $sKey => $sValue) { if(substr_count($sValue,"password(")>0) $aUpdates[]=$sKey.'='.$sValue; //if we are using the password function, don't escape or add quotes. elseif(strtolower($sValue)=="null") $aValues[]="NULL"; //if we are setting a field to NULL, don't escape or add quotes. else $aUpdates[]=$sKey.'="'.$this->objConnection->real_escape_string($sValue).'"'; //escape and add quotes } $sQuery='UPDATE '.$Table.' SET '.implode(",",$aUpdates); if($this->UpdateTimestamps) $sQuery.=',Modified=now() '; $sQuery.=' WHERE '.$Condition; if($Limit) $sQuery.=' LIMIT '.$Limit; $this->LastQuery=$sQuery; $bResult=$this->objConnection->query($sQuery); if(true==$bResult) return true; else throw new Exception("Replace Failed: ".$sQuery." Error: ".$this->objConnection->error); } else throw new Exception("Failed to connect to database."); } catch(Exception $e) { $this->LogError($e->getMessage()); return false; } } private function LogError($ErrorText) { if($this->LogFile!="") { try { $oFile=@fopen($this->LogFile,"a"); if($oFile) { $message=date("m/d/y H:i:s")."\n".$ErrorText."\n--------------------------------------------------------------------\n"; fputs($oFile,$message,strlen($message)); fclose($oFile); } } catch(Exception $e){} } } } ?>