http://dan.flippedweb.com * */ global $DB; class DB { public $host; public $user; public $prefix; public $password; public $database; private $db; function __construct() { global $DB; return ($DB=$this); } function connect($host=NULL, $user=NULL, $pass=NULL, $dbname=NULL) { global $DB; if($host) { $this->host = $host; } if($user) { $this->user = $user; } if($pass) { $this->password = $pass; } if($dbname) { $this->database = $dbname; } if(!$this->connected()) { $this->db = new mysqli($this->host, $this->user, $this->password, $this->database); } // test for errors if (mysqli_connect_errno()) { throw new Exception ("Database connection error: " . mysqli_connect_error() ); } return ($DB=$this); } function connected($host=NULL, $user=NULL, $pass=NULL, $dbname=NULL) { // Don't bother to check if there is no database if(!$this->db) { return false; } return @$this->db->ping; } // Wrap database instance variables public function __get($nm) { return $this->db->$nm; } public function __set($nm, $val) { $this->db->$nm = $val; } public function __isset($nm) { return isset($this->db->$nm); } // Wrap database instance functions public function __call($m, $a) { $this->db->$m(@$a[0], @$a[1], @$a[2], @$a[3], @$a[4], @$a[5], @$a[6], @$a[7]); } // Reference to internal database public function getDB() { return $this->db; } /* * Runs a query * * @param $sql * @param $returnType * 'resource' - returns the mysqli resources directly * 'row' - returns a single row * 'array' - returns all rows as an array */ public function query($sql, $returnType='resource') { // Ensure we are connected to the database $this->connect(); // Let the caller deal with the data if($returnType == 'resource') { if((!$result=$this->db->query($sql))) { @$result->close(); throw new Exception ("Database query error: " . mysqli_error($this->db)); } return $result; } elseif ($returnType == 'row') { if( !($result=$this->db->query($sql, MYSQLI_USE_RESULT)) ) { @$result->close(); throw new Exception ("Database query error: " . mysqli_error($this->db)); } $row = $result->fetch_array(); // Should only have one result if( $result->fetch_array() ) { throw new Exception("More than one row found."); } $result->close(); return $row; } else { // else Return the data as a large array, and fast $resultArray = array(); if(!($result=$this->db->query($sql, MYSQLI_USE_RESULT))) { @$result->close(); throw new Exception ("Database query error: " . mysqli_error($this->db)); } while (($row = $result->fetch_array())) { $resultArray[] = $row; } $result->close(); return $resultArray; } } // Clean up data public function clean($what, $how='') { if($how == '') { return $what; } else if($how == 'serialize') { return serialize(clean($what, 'escape') ); } else if($how == 'file') { if(preg_match('/^[a-zA-Z0-9._-]+$/', $what) == true) { return $what; } else { return NULL; } } else { return $this->db->escape_string($what); } } public function escape($what, $how='') { return $this->clean($what, $how); } public function __destruct() { global $DB; //echo 'DATABASE'; // Re-assign the db connection for barebone usage // For use after this class has been destructed (ie session management) $DB = $this->db; return true; } public function __toString() { return ''; } } ?>