<?php
if(!defined('access')) die ('You are not allowed to execute this file directly.');
/**
* PHP5 MySQL Database Class implementing the Singleton Design Pattern
*
* @package WJ_Tutorials
* @author Chris Strosser
* @link http://webjawns.com/
*/
class pmdb {
/**
* The pmdb database object
*
* @access private
* @var object
*/
private $pmdb;
/**
* MySQLi database object
*
* @access private
* @var object
*/
private static $instance;
/**
* Current result set
*
* @access private
* @var object
*/
private $result;
/**
* The last result (processed)
*
* @access private
* @var array
*/
private $last_result;
/**
* The number of rows from last result
*
* @access private
* @var int
*/
private $row_count;
/**
* Last error
*
* @access private
* @var string
*/
private $last_error;
/**
* PHP5 Constructor
*
* Making this function 'private' blocks this class from being directly created.
*
* @access private
*/
private function __construct($host, $user, $password, $name) {
$this->pmdb = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME)
or die('There was a problem connecting to the database');
self::$instance = $this;
}
/**
* Creates and references the pmdb object.
*
* @access public
* @return object MySQLi database object
*/
public static function connect() {
if ( !self::$instance )
self::$instance = new pmdb();
return self::$instance;
}
/**
* Checks for errors.
*
* @return string|false $last_error if it exists or false if no errors.
*/
public function is_error() {
if ( isset($this->last_error) && !empty($this->last_error) )
return $this->last_error;
return false;
}
/**
* Close active connection to MySQL database.
*
* @access public
* @return bool Always returns true.
*/
public function close() {
if ( $this->pmdb )
$this->pmdb->close();
return true;
}
/**
* Executes query and returns results.
*
* @access public
* @param string $sql The SQL statement to execute.
* @return mixed
*/
public function query($sql) {
$this->result = $this->pmdb->query($sql);
return $this->result;
}
public function get_results($sql) {
if ( !$this->query($sql) )
return false;
$num_rows = 0;
while ( $row = $this->result->fetch_object() ) {
$this->last_result[$num_rows] = $row;
$num_rows++;
}
$this->result->close();
return $this->last_result;
}
public function num_rows() {
return (int) $this->row_count;
}
/**
* Retrieve a single row from the database.
*
* Do not include LIMIT 1 on the end, as this will be taken care
* of automatically.
*
* @param string $sql The SQL statement to execute.
* @return object The MySQL row object
*/
public function get_row($sql) {
if ( !$results = $this->query($sql . " LIMIT 1") )
return false;
return $results->fetch_object();
}
/**
* Sanitizes data for safe execution in SQL query.
*
* @access public
* @param mixed $data The data to be escaped.
* @return mixed
*/
public function escape($data) {
return $this->pmdb->real_escape_string($data);
}
/**
* Prevent cloning of pmdb.
*
* @access public
* @return void
*/
public function __clone() {
// Issue E_USER_ERROR if clone is attempted
trigger_error('Cloning <em>pmdb</em> is prohibited.', E_USER_ERROR);
}
/**
* Destructor
*
* @access public
*/
public function __destruct() {}
}
$user = User::instance();
$current_user = new ACL($_SESSION['userID']);
$pmMailer = new PHPMailer(true);