Source for file DataBaseConnector.class.php
Documentation is available at DataBaseConnector.class.php
* Encapsulates databse functions.
* This class encapsulates all of the functions for connecting to the database.
* Encapsulates database functions..
* This class encapsulates all of the functions for connecting to the database.
/********************************* Public Variables **********************************************/
* Gets or sets whether or not to automaticllay update the Modified and Created fields in the table (must be those exact names).
* Gets or sets the database name.
* Gets or sets the database host. Default is localhost.
public $Host=
"localhost";
* Gets the raw text of the last query performed.
* If set, an error log will be written to this file. Use a full file path.
* Gets or sets the database password.
* Gets or sets the database username.
/********************************* Private Variables **********************************************/
* This is a mysqli class instance.
/********************************* Methods **********************************************/
* Constructor for the class.
* @param string The database username.
* @param string The database password.
* @param string The database name.
* @param string The database host.
function __construct($Username=
"",$Password=
"",$DatabaseName=
"",$Host=
"")
if(isset
($Host)) $this->Host=
$Host;
else $this->Host=
"localhost";
if(isset
($Username)) $this->Username=
$Username;
if(isset
($Password)) $this->Password=
$Password;
* Destructor for the class.
if($this->objConnection) $this->objConnection->Close();
* Connects to the database. Returns true on success, or false on failure.
private function Connect($bRedirect=
false)
//make sure to release any previous connection.
unset
($this->objConnection);
//create a new mysqli class and connect.
//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
//no error, so return the object and store a reference.
$this->objConnection=
$objConnection;
$this->LogError("MySQL Connection Failure: ".
$e->getMessage());
* 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.
public function Escape($Value)
return($this->objConnection->real_escape_string($Value));
* Returns the number of rows affected by the previous query
if(!$this->objConnection) return 0;
return($this->objConnection->affected_rows);
* Gets the text of the last error, if any.
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.
public function Insert($Table,$Fields)
if(!is_array($Fields)) throw
new Exception("Attempted to insert with no fields.");
foreach($Fields as $sKey =>
$sValue)
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);
$sQuery.=
') VALUES ('.
implode(',',$aValues);
$bResult=
$this->objConnection->query($sQuery);
if(true==
$bResult) return($this->objConnection->insert_id);
else throw
new Exception("Insert Failed: ".
$sQuery.
" Error: ".
$this->objConnection->error);
$this->LogError($e->getMessage());
* 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.
public function Query($Query)
$oResult=
$this->objConnection->query($Query);
if(false==
$oResult) throw
new Exception("Query failed: $query Error: ".
$objConnection->error);
else throw
new Exception("Connection Failed.");
$this->LogError($e->getMessage());
* 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.
public function Replace($Table,$Fields,$Delayed=
false)
if(!is_array($Fields)) throw
new Exception("Attempted to update with no fields selected");
foreach($Fields as $sKey =>
$sValue)
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);
$sQuery.=
') VALUES ('.
implode(',',$aValues);
$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.");
$this->LogError($e->getMessage());
* 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.
public function Select($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);
else throw
new Exception("Failed to connect to database.");
$this->LogError($e->getMessage());
* Tests to make sure the connection to the database is open, and if not, attempts to re-open the connection.
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.");
$this->LogError($e->getMessage());
* 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.
public function Update($Table,$Fields,$Condition,$Limit=
0)
if(!is_array($Fields)) throw
new Exception("Tried to update with an empty field list.");
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);
$sQuery.=
' WHERE '.
$Condition;
if($Limit) $sQuery.=
' LIMIT '.
$Limit;
$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.");
$this->LogError($e->getMessage());
private function LogError($ErrorText)
$message=
date("m/d/y H:i:s").
"\n".
$ErrorText.
"\n--------------------------------------------------------------------\n";
Documentation generated on Wed, 26 Jan 2011 15:24:43 -0700 by phpDocumentor 1.4.3