<?php
/************************************
 * Asiel Brumfield
 * weber state cs4350
 * the class to handle db dbs for project5
 * NOTE DEPENDENCIES
 * 1. MDB2 PEAR package
 * 2. MDB2_Driver_mysql PEAR package
 *
 * Reference Documentation
 * http://pear.php.net/manual/en/package.database.mdb2.php
 ************************************/

require_once 'MDB2.php';

class 
dbConnection
{
    private 
$dbName;
    private 
$user;
    private 
$host;
    private 
$dbtype 'mysql';
    private 
$db;
    private 
$connected false;
    
    function 
__construct($user$pass$host$dbName)
    {        
        
$this->user $user;
        
$this->pass $pass;
        
$this->host $host;
        
$this->dbName $dbName;        
    }
    
    public function 
connect()
    {
        
$dsn['username'] = $this->user;
        
$dsn['password'] = $this->pass;
        
$dsn['hostspec'] = $this->host;
        
$dsn['phptype'] = $this->dbtype;
            
        
$this->db =& MDB2::factory($dsn);
        
# if the db wasn't successful say why
        
if (PEAR::isError($this->db))
        {            
            die(
__LINE__.$this->db->getMessage());
        }
        
$this->db->setOption('result_buffering'true);
        
$this->db->setFetchMode(MDB2_FETCHMODE_ASSOC);
        
$this->db->loadModule('Extended');
        
# switch to the appropriate db
        
$this->db->setDatabase($this->dbName);
        
# identify that we have a connection
        
$this->connected true;
        return 
$this->db;
    }
}

###############################################################################
# This is how we could do a simple connection
# in the older less portable fashion using mysql_connect
#    mysql_connect('localhost', '4350', 'wsuslcc');
#    mysql_select_db('project5');
#    $result = mysql_query('show tables');
#    for ($i=0; $i<mysql_num_rows($result); $i++)
#    {    
#        $array[] = mysql_fetch_assoc($result);
#    }
#    print_r($array);
###############################################################################

?>