| 
<?php
 /* Class name       : Database
 * Inherited from   :
 * Created by       : Junaid Hassan (email : [email protected] , 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;
 }
 }
 
 }
 
 ?>
 
 |