Class DataBaseConnector

This class encapsulates all of the functions for interacting with a database.

  • author: Richard Sharp
  • version: 3.0
  • copyright: 2006-2010
Variable Summary
string $sDatabase
string $sErrorLog
string $sHost
string $sLastQuery
string $sPassword
string $sUser
Method Summary
void DataBaseConnector ([string $User = ""], [string $Password = ""], [string $Database = ""], [string $Host = ""])
string Escape (string $value)
string GetLastError ()
mixed Insert (string $sTable, array $aFields)
mixed Query (string $sQuery)
bool Replace (string $sTable, array $aFields, [boolean $bDelayed = false])
mixed Select (string $sQuery)
bool TestConnection ()
bool Update (string $sTable, array $aFields, string $sCondition, [int $iLimit = 0])
Variables
boolean $bUpdateTimestamps = true (line 18)

Gets or sets whether or not to automaticllay update the Modified and Created fields in the table (must be those exact names).

string $sDatabase = "" (line 24)

Gets or sets the name of the database.

string $sErrorLog = "" (line 36)

If a value is provided, all errors will be written to this filename if possible.

string $sHost = "" (line 30)

Gets or sets the hostname of the database. If not provided, localhost will be assumed.

string $sLastQuery = "" (line 42)

Gets the SQL text of the last query processed.

string $sPassword = "" (line 48)

Gets or sets the database password.

string $sUser = "" (line 54)

Gets or sets the database username.

Methods
Constructor DataBaseConnector (line 73)

Constructor for the class.

void DataBaseConnector ([string $User = ""], [string $Password = ""], [string $Database = ""], [string $Host = ""])
  • string $User: The database username.
  • string $Password: The database password.
  • string $Database: The database name.
  • string $Host: The database host.
Escape (line 93)

Escapes the value to be placed in a field. This helps prevent problems with quotes, and also prevents SQL injection attacks.

  • return: Escapped string.
string Escape (string $value)
  • string $value: The value to escape.
GetAffectedRows (line 106)

Returns the number of rows affected by the last query

  • return: The number of affected rows.
int GetAffectedRows ()
GetLastError (line 118)

Gets the text of the last error, if any.

  • return: The text of the last error, if any.
string GetLastError ()
Insert (line 132)

Inserts a record into the database.

  • return: The inserted row ID on success, false on fail.
mixed Insert (string $sTable, array $aFields)
  • string $sTable: The name of the table. Returns the insert auto-incremented ID.
  • array $aFields: 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";
Query (line 184)

Runs a query on the database that does not expect any return.

  • return: The result object of the query.
mixed Query (string $sQuery)
  • string $sQuery: The query to run.
Replace (line 208)

Performs an Update query on the database. Returns success or failure.

  • return: Returns true on success, false on failure.
bool Replace (string $sTable, array $aFields, [boolean $bDelayed = false])
  • string $sTable: The name of the table.
  • array $aFields: 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.
  • boolean $bDelayed: Whether or not to use delayed replace. Default false.
Select (line 261)

Runs a Select query on the database. Returns an associative array of rows.

  • return: On success, a numbered array of result rows. Each array element is an associative array of the results. False on failure.
mixed Select (string $sQuery)
  • string $sQuery: The SQL query to run.
TestConnection (line 294)

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.

bool TestConnection ()
Update (line 316)

Performs an Update query on the database. Returns success or failure.

  • return: True on success, false on failure.
bool Update (string $sTable, array $aFields, string $sCondition, [int $iLimit = 0])
  • string $sTable: The name of the table.
  • array $aFields: 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.
  • string $sCondition: The WHERE clause of the query. Required. Use "1=1" to update all fields.
  • int $iLimit: Optional. The limit to the number of rows that should be affected.