<?php
//==================================================================================
// An object oriented general purpose DBio object
//==================================================================================
class DBio
{
    private $host;
    private $port;
    private $DBname;
    private $user;
    private $pswd;
    
//-------------------------------------------------------------------------------
// DBio: a constructor to establish connection info to a database
//     args: host_port - the host where the DB server is running
//           port      - the port number (optional) 
//           DBname    - the database name
//           DBuser    - the userid
//           DBpswd    - the password
//------------------------------------------------------------------------------- 
    function __construct($host,$port,$db,$user,$pswd)                               
    {
        $this->host     = $host;        //establish database connect info
        $this->port     = $port;        
        $this->DBname   = $db;                          
        $this->user     = $user;                        
        $this->pswd     = $pswd;                        
    }

//-------------------------------------------------------------------------------
// getters and setters for private object properties
//------------------------------------------------------------------------------- 
    function __get($name)               //will automatically be used if property is private
    {
        return($this->$name);           //return the value of the property                                      
    }

    function __set($name, $value)       //will automatically be used if property is private
    {
        $this->$name = $value;          //set the value of the property
    }   

//-------------------------------------------------------------------------------------
// process: a method to process the enclosed sql statement
//     args:  sql - the sql statement
//   return:  either the number of rows effected (if insert/update/delete/ddl), or
//            a 2 dim associative array which contains the result of a SELECT query 
//-------------------------------------------------------------------------------------
    function process($sql)
    {    
        $connect = new mysqli($this->host,$this->user,$this->pswd,$this->DBname,$this->port);    
        if (! $connect) 
            die('Could not connect: ' . $connect->connect_error);

        $cursor = $connect->query($sql);                        #execute sql                    
        if (! $cursor) 
            die('Could not execute sql: ' . $connect->error);
     
        if (is_object($cursor))                                 #must be SELECT statement
        {       
            while ($row = $cursor->fetch_array())               #get each row as an array
                $data[ ] = $row;                                #store row in 2 dim array    

            $cursor->free_result();                             #free result buffer
        }
        else
            $data = $connect->affected_rows;                    #number of rows affected 

        $connect->close();                                      #close connection

        return($data);
    }
//-------------------------------------------------------------------------------------- 
}       //end of class - Do not delete this line
//-------------------------------------------------------------------------------------- 
?>