Content.exe
Updated the database class
Usage:
// min instantiation
$dataBase = new DB('hostname','username','password','database');
// max instantiation
$dataBase = new DB('hostname','username','password','database',DEBUG_LEVEL,array('admin@domain.com','admin@domain2.com));
Class:
class DB{
/*
* DESCRIPTION: Object class, Handles all database requests and returns content accordingly
* WRITER: Adrian Callaghan 12,08,08
* UPDATED: Adrian Callaghan 08,08,11
*
*
* errLvl
* (0), none,
* (1) show a user error,
* (2) (1) + mysql error
* (3) (2) + show a full error (mysql errors),
* (4) (3) + plus debugging
*/
private $host, $usr, $pwd, $errLvl, $mySqlHost, $dataBase, $emailAddresses;
public function __construct($host, $usr, $pwd, $db, $errLvl=0, $adminEmails=array()) {
/*
* Sets up a new connection
*/
// MIN required
$this->setHost($host);
$this->setUsr($usr);
$this->setPwd($pwd);
$this->setErrLvl($errLvl); // sets defualt RTE
$this->setEmailErrsTo($adminEmails);
// connect
$this->connect();
$this->setDb($db);
}
public function __destruct(){
/*
* Close the connection
*/
$this->disconnect();
}
protected function setHost($host='localhost'){
/*
* Sets the hostname
*/
$this->host = $host;
}
protected function setUsr($usr=''){
/*
* Sets the username
*/
$this->usr = $usr;
}
protected function setPwd($pwd=''){
/*
* Sets the password
*/
$this->pwd = $pwd;
}
protected function setErrLvl($errLvl=0){
/*
* Sets the error reporting level
*/
$this->errLvl = $errLvl;
}
protected function connect(){
/*
* Connect to server
*/
$this->mySqlHost = @mysql_connect($this->host, $this->usr, $this->pwd);
if (!$this->mySqlHost) {
$this->error('Cannot connect to server',mysql_error());
}
}
protected function disconnect() {
/*
* closes current connection
*/
@mysql_close($this->mySqlHost);
}
public function setDb($db){
/*
* Set the database
*/
$this->dataBase = @mysql_select_db($db,$this->mySqlHost);
if (!$this->dataBase) {
$this->error('Cannot select database',mysql_error());
}
}
public function setEmailErrsTo($email = array()){
// ensure it is an array
$email = !is_array($email) ? array($email) : $email;
// if empty return
if (empty($email)){
return;
}
// assign the email addresses
$this->emailAddresses = $email;
}
function getQuery($SQL){
// executes a query
$result = @mysql_query($SQL);
if (!$result) {
$this->error('Invalid query',mysql_error(),$SQL);
}
// create a 2D array of results
$return = array();
while ($row = mysql_fetch_assoc($result)) $return[] = $row;
return $return;
}
function setQuery($SQL){
// executes a query without returning any results, used for insert, create etc, returns the ID of the last auto-increment
$result = @mysql_query($SQL);
if (!$result) {
$this->error('Invalid query',mysql_error(),$SQL);
}
$return = mysql_insert_id();
return $return;
}
public function error($message, $mysqlErr='', $SQL=''){
/*
* Handles errors
*
*/
switch ($this->errLvl){
case 0: break;
case 1: $mysqlErr=''; // kill the mysql output
case 2: $SQL=''; // kill the sql output
case 3:
case 4:
echo "$message
";
if ($mysqlErr!=='') {
echo "Mysql: $mysqlErr
";
}
if ($SQL!=='') {
echo "Whole query: $SQL
";
}
}
$email = $this->generateEmail(array('Error'=>$message,'Mysql'=>$mysqlErr,'Sql'=>$SQL,'Err Level'=>$this->errLvl));
// three is for debugging
if ($this->errLvl>3){
echo $email;
}
if (!empty($this->emailAddresses)){
$sent = $this->sendEmails($email);
if ($sent) echo 'The administrator has been notified
';
else {
echo 'Please notify the administrator';
echo count($this->emailAddresses)>1 ? "'s " : " ";
echo implode(' or ',$this->emailAddresses);
echo '
';
}
}
exit();
}
private function generateEmail($err=array()){
$fields = array(
'Server name'=>$_SERVER['SERVER_NAME'],
'Query'=>$_SERVER['SCRIPT_FILENAME'].'?'.$_SERVER['QUERY_STRING'],
'Ip'=>$_SERVER['REMOTE_ADDR'].':'.$_SERVER['REMOTE_PORT'],
'@'=>date("D dS M,Y h:i a")
);
$rtn = '
$title | $value |