database.php
Keegan's picture
Published on Fri, Nov 07, 2014 by Keegan.

The database class handles the connection to and disconnection from the database. The database class also handles the insertion, deletion, updating, and retrieving of data from the database. Furthermore, the database class will do basic sanitizing of data.

The database class is the class to use when using the database. It passes queries to the database adapter. Please note that all example queries are written for a MySQL-based database server.

It is also important to note that data from the database may not be sanitized or verified.

Requirements:

Code:

class database implements databaseInterface

public static function getInstance()
Returns an instance of the database object. If one doesn't exist one will be created.

public static function getRequiredPHPDatabaseModule()
Returns an empty string. Only here to meet the requirements set by the database interface.

private function __construct()
Sets database connection settings., then configures and connects the database adapter.

private function __clone()
Empty. Here to prevent clones of the database engine.

public function __destruct()
Disconnects the database before being destroyed.

public function isConnected()
Returns true if the adapter is connected to the database, false if not.

public function bootstrapDisconnect()
The function called by bootstrap to disconnect the adapter from the database.

public function disconnect()
Disconnects the adapter from the database. The only time disconnect should be called is by bootstrap and when the database object is destroyed.

public function getData($select, $from[, $where = '1'])
Gets data from the database and returns a two-dimensional array of the retrieved data, false on error, or null if no data was retrieved. The array is formed like $arrayName[$rowNumber][$columnName].

  • Our example query passed to the database is "SELECT n.title, u.firstName FROM node n, user u WHERE n.authorID = u.userID AND n.nodeID = 1"
  • $select - the select part of a SQL query without the SELECT keyword. Example: "n.title, u.firstName"
  • $from - the from part of a SQL query without the FROM keyword. Example: "node n, user u"
  • $where - the where clause of a SQL query without the WHERE keyword. Example: "n.authorID = u.userID AND n.nodeID = 1"; defaults to 1.

public function makeCustomQuery($inQuery)
Runs a custom query on the database and returns the results. Returns false on error. $inQuery is the custom query to run.

public function insertData($into, $columns, $values)
Inserts data into the database. Returns true on success and false on failure.

  • Our example query passed to the database is "INSERT INTO statusSupporter (supporterID, statusID) VALUES (4, 48932)"
  • $into - the insert into part of a SQL query without the INSERT INTO keyword. Example: "statusSupporter"
  • $columns - a comma-seperated list of columns to insert data into. Example: "supporterID, statusID"
  • $values - a comma-seperated list of the data to insert. Example: "4, 48932"

public function updateTable($table, $set, $values)
Updates rows in the database. Returns true on success and false on failure.

  • Our example query passed to the database is "UPDATE messageRecipient SET messageID=32 WHERE id=2"
  • $table - the table to update without the UPDATE keyword. Example: "messageRecipient"
  • $set - a comma-seperated list of columns and values to update. Example: "messageID=32"
  • $values -the where clause of the SQL query without the WHERE keyword. Example: "id=2"

private function connect()
Instructs the database adapter to connect to the database server.

public function configure($dbServer, $userName, $password, $db)
Doesn't do anything. Just here to meet requirements set by the database interface.

public function escapeString($inString)
Sanitizes the inputted string and returns a safe (theoritical, always check!) string to use in database queries.

public function removeData($from, $where)
Removes data from the database. Returns true on success and false on failure.

  • Example query: "DELETE FROM mailTemplate WHERE id=45"
  • $from - the table to delete from. Example: "mailTemplate"
  • $where - the where part of the SQL query without the WHERE keyword. Example: "id=45"

public function getError()
Returns the last error to happen on a query.

public function getLastInsertID()
Returns the last unique identifier to be inserted into the database.

Example:

require_once(DATABASE_OBJECT_FILE);
if (! $database->isConnected()) {
die('Not connected');
}
$data = $database->getData('title', 'node', 'authorID=57');
if($data==false) {
die('Error occured');
}
if($data==null) {
die('No data');
}
foreach($data as $entry) {
echo $entry['title'];
}