PHP Classes
elePHPant
Icontem

File: code/includes/database.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Junaid Hassan  >  PHP MVC  >  code/includes/database.php  >  Download  
File: code/includes/database.php
Role: Class source
Content type: text/plain
Description: database.php
Class: PHP MVC
MVC framework that loads XML configuration files
Author: By
Last change:
Date: 1 year ago
Size: 2,238 bytes
 

Contents

Class file image Download
<?php

/* Class name : Database
 * Inherited from :
 * Created by : Junaid Hassan (email : junaidhassanalvi@gmail.com , blog : junaidhassanalvi.wordpress.com)
 * Created On : 17-April-2103
 * Description : This class is created to handle all database operations
 * This is for mysql. Other classes could be written for other drivers
 *
 * Change Logs :
 *
 */
class Database {

    private
$con, $rs;
    protected
$catalog;

    function
__construct($catalog) {
       
$this->catalog = $catalog;
    }

   
//jha-- connect to database, according to the credentials provided in configuration file
   
public function connect() {

        if (
is_resource($this->con))
            return;
       
$config = $this->catalog->get('config');
       
$this->con = mysql_connect($config->database->host, $config->database->username, $config->database->password);
        if (!
is_resource($this->con))
            die(
'Unable to connect to dabase server');
       
$db_selected = mysql_select_db($config->database->dbname, $this->con);
        if (!
$db_selected) {
            die(
'Can\'t use Specified database');
        }
    }
   
   
//jha-- execute provided sql statement
    //jha-- sent result back in a form of array
   
public function execute($sql) {
       
$this->connect();
       
$this->rs = mysql_query($sql, $this->con);
        return array(
'errno' => mysql_errno($this->con), 'error' => mysql_error($this->con));
    }

   
//jha-- return desire object of information
    //jha-- according to the type provided
   
public function fetch($type = 'array') {

        switch (
$type) {
            case
'array' :
                return
mysql_fetch_array($this->rs);
                break;
            case
'row' :
                return
mysql_fetch_row($this->rs);
                break;
            case
'object' :
                return
mysql_fetch_object($this->rs);
                break;
            case
'record set' :
                return
$this->rs;
                break;
             case
'count' :
                return
mysql_num_rows($this->rs);
                break;
        }
    }

}

?>