Source for file DataBaseConnector.class.php
-
-
-
* This class encapsulates all of the functions for interacting with a database.
-
-
-
-
-
-
-
-
-
/********************************* Public Variables **********************************************/
-
-
-
* Gets or sets whether or not to automaticllay update the Modified and Created fields in the table.
-
-
-
-
-
-
* Gets or sets the name of the database.
-
-
-
-
-
-
* Gets or sets the hostname of the database. If not provided, localhost will be assumed.
-
-
-
-
-
-
* If a value is provided, all errors will be written to this filename if possible.
-
-
-
-
-
-
* Gets the SQL text of the last query processed.
-
-
-
-
-
-
* Gets or sets the database password.
-
-
-
-
-
-
* Gets or sets the database username.
-
-
-
-
-
/********************************* Private Variables **********************************************/
-
-
-
-
-
-
-
/********************************* Public 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.
-
-
-
-
-
if($Host!=
"") $this->sHost=
$Host;
-
-
else $this->sHost=
"localhost";
-
if($User!=
"") $this->sUser=
$User;
-
-
if($Password!=
"") $this->sPassword=
$Password;
-
-
if($Database!=
"") $this->sDatabase=
$Database;
-
-
-
-
-
-
* Escapes the value to be placed in a field. This helps prevent problems with quotes, and also prevents
-
-
* @param string The value to escape.
-
* @return string Escapped string.
-
-
-
-
-
-
-
-
-
-
-
-
* Returns the number of rows affected by the last query
-
* @return int The number of affected rows.
-
-
-
-
-
-
-
-
-
-
-
* Gets the text of the last error, if any.
-
* @return string The text of the last error, if any.
-
-
-
-
-
-
-
-
-
-
-
* Inserts a record into the database.
-
* @param string The name of the table. Returns the insert auto-incremented ID.
-
* @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. Ex: $fields["FirstName"]="John";
-
* @return mixed The inserted row ID on success, false on fail.
-
-
function Insert($sTable,$aFields)
-
-
//validate the passed information
-
if(!$sTable ||
($sTable==
''))
-
-
$this->_logError("Attempted to insert record with missing table name.");
-
-
-
-
-
$this->_logError("Attempted to insert record with missing fields.");
-
-
-
-
-
foreach($aFields as $sKey =>
$sValue)
-
-
-
-
//if this is a password value, don't escape
-
-
//if the value is NULL, use "NULL"
-
elseif(strtolower($sValue)==
"null") $aValues[]=
"NULL";
-
-
-
-
-
-
$sQuery=
'INSERT INTO '.
$sTable.
' ('.
implode(",",$aKeys);
-
-
$sQuery.=
') VALUES ('.
implode(',',$aValues);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
* Runs a query on the database that does not expect any return.
-
* @param string The query to run.
-
* @return mixed The result object of the query.
-
-
-
-
-
if(!$sQuery ||
($sQuery==
''))
-
-
$this->_logError("Attempted to run a blank raw query.");
-
-
-
-
-
-
-
if(!$oResult) $this->_logError();
-
-
-
-
-
-
* 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 boolean Whether or not to use delayed replace. Default false.
-
* @return bool Returns true on success, false on failure.
-
-
function Replace($sTable,$aFields,$bDelayed=
false)
-
-
//check for required information
-
if(!$sTable ||
($sTable==
''))
-
-
$this->_logError("Attempted to replace records with missing table name.");
-
-
-
-
-
$this->_logError("Attempted to replace records with missing fields.");
-
-
-
-
-
-
foreach($aFields as $sKey =>
$sValue)
-
-
-
-
//if this is a password value, don't escape
-
-
//if the value is NULL, use "NULL"
-
elseif(strtolower($sValue)==
"null") $aValues[]=
"NULL";
-
-
-
-
-
-
$sQuery=
'REPLACE'.
($bDelayed?
" DELAYED":
"").
' INTO '.
$sTable.
' ('.
implode(",",$aKeys);
-
-
$sQuery.=
') VALUES ('.
implode(',',$aValues);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
* Runs a Select query on the database. Returns an associative array of rows.
-
* @param string The SQL query to run.
-
* @return mixed On success, a numbered array of result rows. Each array element is an associative array of the results. False on failure.
-
-
-
-
//check that a query was sent
-
if(!$sQuery ||
($sQuery==
''))
-
-
$this->_logError("Attempted to run a Select with no query.");
-
-
-
-
-
-
-
-
-
-
-
-
//loop through each value and strip out slashes
-
-
foreach($aRow as $sKey =>
$sValue) $this_row[$sKey]=
stripslashes($sValue);
-
-
-
-
-
-
-
-
-
-
-
* Tests to make sure the connection to the database is open, and if not, attempts to re-open the connection. This prevents unnecessary connections to the database.
-
-
-
-
-
//if no connection resource, attempt to connect
-
if(!$this->_objConnection) $this->_connect();
-
-
//if still no conenction resource, connection failed
-
if(!$this->_objConnection) return false;
-
-
//ping the conection to test and reactivate if necessary
-
if(!mysql_ping($this->_objConnection)) 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. Required. Use "1=1" to update all fields.
-
* @param int Optional. The limit to the number of rows that should be affected.
-
* @return bool True on success, false on failure.
-
-
function Update($sTable,$aFields,$sCondition,$iLimit=
0)
-
-
//check for required information
-
if(!$sTable ||
($sTable==
''))
-
-
$this->_logError("Attempted to update records with missing table name.");
-
-
-
-
-
$this->_logError("Attempted to update records with missing fields.");
-
-
-
if(!$sCondition ||
($sCondition==
''))
-
-
$this->_logError("Attempted to update records with missing condition.");
-
-
-
-
-
-
foreach($aFields as $sKey =>
$sValue)
-
-
//if this is a password value, don't escape
-
if(substr_count($sValue,"password(")>
0) $aUpdates[]=
$sKey.
'='.
$sValue;
-
//if the value is NULL, use "NULL"
-
elseif(strtolower($sValue)==
"null") $aValues[]=
"NULL";
-
-
-
-
-
-
$sQuery=
'UPDATE '.
$sTable.
' SET '.
implode(",",$aUpdates);
-
-
$sQuery.=
' WHERE '.
$sCondition;
-
if($iLimit) $sQuery.=
' LIMIT '.
$iLimit;
-
-
-
-
-
if(!$ok) $this->_logError();
-
-
-
-
-
/************ Private Functions *************/
-
-
-
* Connects to the database.
-
-
* @return boolean True if successful, otherwise false.
-
-
-
-
//make sure that the connection authentication values have been set
-
-
-
-
-
-
$this->_logError("Attempted to connect with missing authentication information.");
-
-
-
-
//connect to the database
-
-
-
//if connected, select the database
-
-
-
-
-
-
$this->_objConnection=
$_objConnection;
-
-
-
-
-
-
-
-
-
* Logs errors to an external text error log. Only if sErrorLog is set
-
-
* @param string The message to store. If not set, the last query and database error will be stored.
-
-
-
function _logError($message)
-
-
-
-
-
-
-
$message=
date("m/d/y H:i:s").
"\n".
$message.
"\n--------------------------------------------------------------------\n";
-
-
-
-
-
-