Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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();
- }
- }
- ?>
Add Comment
Please, Sign In to add comment