Skip to content

Instantly share code, notes, and snippets.

Created March 17, 2011 17:49
Show Gist options
  • Save anonymous/874784 to your computer and use it in GitHub Desktop.
Save anonymous/874784 to your computer and use it in GitHub Desktop.
<?php
class DB {
private $host;
private $user;
private $pass;
private $database;
private $connection;
public $result;
public function __construct() {
$this->load_config();
try {
$this->connect();
}
catch(Exception $e) {
echo $e->getMessage;
exit;
}
}
private function load_config() {
$config = parse_ini_file("config.ini");
$this->host = $config['host'];
$this->user = $config['user'];
$this->pass = $config['pass'];
$this->database = $config['database'];
}
private function connect() {
$this->connection = @mysql_connect($this->host, $this->user, $this->pass);
if(!$this->connection) {
throw new Exception("<b>ERROR:</b> Failed to establish MySQL connection!");
}
$db = @mysql_select_db($this->database, $this->connection);
if(!$db) {
throw new Exception("<b>ERROR:</b> Failed to select database!");
}
}
public function query($query) {
if(is_resource($this->result)) {
mysql_free_result($this->result);
}
$this->result = @mysql_query($query, $this->connection);
if(!$this->result) {
throw new Exception("<b>ERROR:</b> Failed to execute query!");
}
}
private function disconnect() {
if(is_resource($this->result)) {
mysql_free_result($this->result);
}
if(is_resource($this->connection)) {
mysql_close($this->connection);
}
}
public function __destruct() {
$this->disconnect();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment