<?
 
 
/* 
 
    Pork dbConnection object.
 
*/
 
 
class dbConnection
 
{
 
    var $num_rows, $affected_rows, $connection, $timer, $exectime, $result, $lastquery, $error, $output, $limit, $username, $password, $database,$queries, $func, $host;
 
 
        
 
    /**
 
        constructor: read settings and connects.
 
     */
 
    function __construct($settingsfile='./settings/dbsettings.php')
 
    {
 
        $this->queryCount = 0;
 
        $this->readSettings($settingsfile);
 
        $this->connect();
 
    }
 
    
 
    /*
 
    Reads the settings file. It's supposed to have this format:
 
    <\?
 
    die();
 
    dbtype = mysql
 
    host = hostname:3306
 
    username = your_user_name
 
    password = your_password
 
    database = your_database
 
    func = mysql_fetch_object
 
    ?\>
 
 
    */
 
    function readSettings($file)
 
    {
 
        $input = file_get_contents($file);
 
        $file = explode("\n", $input );
 
        for ($i=2; $i<sizeof($file) -1; $i++)
 
        {
 
            $property = explode ("=", $file[$i]);
 
            $prop = trim($property[0]);
 
            $this->$prop = trim($property[1]);
 
        }
 
    }
 
 
    /**
 
     * Connects to the database.
 
     */
 
    function connect()
 
    {
 
        $this->connection = mysql_connect($this->host, $this->username, $this->password);
 
        if ($this->connection)
 
        {
 
            mysql_select_db($this->database, $this->connection);
 
            return true;
 
        }
 
        return false;
 
    }
 
 
 
    
 
    /**
 
     * Zoekt finds the number of rows returned.
 
     */
 
    function numrows()
 
    {
 
        return @mysql_num_rows($this->result);
 
    }
 
 
        
 
    /**
 
     * Excecutes the query and processes: 
 
     * @ insert: MYSQL_INSERT_ID
 
     * @ delete, replace, or update: MYSQL_AFFECTED_ROWS;
 
     * @ select: MYSQL_NUM_ROWS
 
     */
 
    function query($query)
 
    {
 
        $this->queries[]= $query;
 
        $this->insertID = 0;
 
        $this->lastQuery = $query;
 
        if(!$this->connection && !$this->connect()) { return false; }
 
        mysql_query("use {$this->database}", $this->connection); 
 
        $this->result = mysql_query($this->lastQuery, $this->connection);
 
        $this->error =  mysql_error($this->connection);
 
        $query = strtolower($this->lastQuery);
 
        if (empty($this->error))
 
        {
 
            $this->num_rows = 0;
 
            $this->affected_rows = 0;
 
            if (strpos($query, 'insert') !== false) 
 
            {
 
                $this->insertID = mysql_insert_id($this->connection);
 
            }
 
            elseif (strpos($query, 'delete') !== false || strpos($query, 'replace') !== false || strpos($query, 'update') !== false)
 
            {
 
                $this->affected_rows = mysql_affected_rows($this->connection); 
 
            }
 
            else
 
            {
 
                $this->num_rows = $this->numrows();
 
            }
 
            $this->queryCount++;
 
            if (!empty($this->insertID)) { return ($this->insertID); }
 
        }
 
        else
 
        {
 
         print_r($this->error."<br>While executing query: <br><span style='color:green;'>{$query}</span>");
 
         return false;
 
        }
 
        return true;        
 
    }
 
 
    /**
 
        Fetches a single result form the current result set or a passed query
 
    */
 
    function fetchOne($query='')
 
    {
 
        if ($query != '') $okay = $this->query($query);
 
        $this->output = ($okay && $this->num_rows > 0) ? mysql_result($this->result, 0) : false;
 
        return($this->output);
 
    }
 
 
    /**
 
     * Executes the passed query using $func and / or fetches a multi dimensional array of results.
 
     */
 
    function fetchAll($query='', $func=false)
 
    {
 
        $output = array();
 
        $okay = ($this->output != false);
 
        $okay = ($query != '') ? $this->query($query) : $okay;
 
        if ($okay)
 
        {
 
            $func = ($func != false) ? $func : $this->func;
 
            while ($array = $func($this->result)) $output[] = $array;
 
            $this->numrows = sizeof($output);
 
            $this->output = $output;
 
        }
 
        return($output);
 
    }
 
 
    /**
 
     * Executes the passed query using $func and / or fetches one row of results..
 
     */
 
    function fetchRow($query='', $func=false)
 
    {
 
        $output = array();
 
        $okay = ($this->output != false);
 
        $okay = ($query != '') ? $this->query($query) : $okay;
 
        if ($okay)
 
        {
 
            $func = ($func != false) ? $func : $this->func;
 
            if ($array = $func($this->result)) $output = $array;
 
            $this->output = $output;
 
        }
 
        return $output;
 
    }
 
 
    function getNumRows()
 
    {
 
        return ($this->num_rows);
 
    }
 
 
    function getAffectedRows()
 
    {
 
        return ($this->affected_rows);
 
    }
 
 
    function setPassword($val)
 
    {
 
        $this->password = $val;
 
    }
 
 
    function setUsername($val)
 
    {
 
        $this->username = $val;
 
    }
 
 
    function setHost($val)
 
    {
 
        $this->host = $val;
 
    }
 
 
    function setDatabase($val)
 
    {
 
        $this->database = $val;
 
        if ($this->connection)
 
        {
 
            mysql_select_db($val, $this->connection);
 
        }
 
    }
 
 
    function getError()
 
    {
 
        return($this->error);
 
    }
 
 
    function getLastQuery()
 
    {
 
        return($this->lastquery);
 
    }
 
 
    function __destruct() 
 
    {
 
        mysql_close($this->connection);
 
    }
 
 
}
 
 
 
?>
 
 |