View Single Post
Old 12-28-2008, 12:46 PM   #3 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Not trying to take away Orc's job here, but I have this database class, perhaps it could be used? Or used as a base, and someone could modify it for the better?

php Code:
<?php

/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/


    class DBmysql implements iDB {
       
        private $host;
        private $user;
        private $pass;
        private $data;
       
        private $tables = array();
        private $rows = array();
       
        private $query_sql;
        private $query_result;   
        private $query_fetch = array();
       
       
        public function setHandler($array) {
           
            $this->host = $array['host'];
            $this->user = $array['user'];
            $this->pass = $array['pass'];
            $this->data = $array['data'];
           
            return $this;
           
        }
       
        public function connect() {
           
            @mysql_connect($this->host, $this->user, $this->pass) or die("Could not connect to the database.");
           
            return $this;
           
        }
       
        public function select() {
           
            @mysql_select_db($this->data) or die("Could not select database.");
           
            return $this;
           
        }
       
        public function disconnect() {
           
            @mysql_close() or die("Could not close database connection. Perhaps because the connect isn't opened.");
           
        }
       
        public function loadQuery($query) {
           
            $this->query_sql = $query;
           
            return $this;
           
        }

        public function exeQuery()
        {
           
            if(isset($this->query_sql))
            {
               
                $this->query_result = @mysql_query($this->query_sql);
               
                if(!$this->query_result)
                {
                   
                    throw new Exception('An error occured while querying: ' . mysql_error());
                   
                }
               
                else return $this;
               
            }
           
            else throw new Exception('No query is loaded.');
           
        }
       
        public function getQueryResult()
        {
           
            if(isset($this->query_result))
            {
               
                return $this->query_result;
               
            }
           
            else throw new Exception('No query has been executed.');
           
        }
       
        function loadFetch( $assoc = false, $sql = null )
        {
       
            unset( $this->query_fetch );
            empty( $this->query_fetch );
       
            $sql = (is_null($sql) && isset($this->query_result)) ? $this->query_result : $sql;
            $fetchType = ($assoc == true) ? MYSQL_ASSOC : MYSQL_NUM;
       
            if(!empty($sql))
            {
               
                while($fetch = mysql_fetch_array($sql, $fetchType))
                {
                   
                    $this->query_fetch[] = $fetch;
                   
                }
       
                if(!is_array($this->query_fetch))
                {
                   
                    throw new Exception("An error occured while fetching the array" . mysql_error());
                    
                }
   
                else
                {
                   
                    return $this;
                    
                }
                
            }
       
        }

        public function getFetch()
        {
           
            if(is_array($this->query_fetch) && isset($this->query_fetch))
            {
               
                return $this->query_fetch;
               
            }
           
            else throw new Exception('No fetch has been loaded.');
           
        }
       
        public function secure($string) {
           
            $escapedstring = mysql_real_escape_string($string);
           
            return $secure;
                       
        }
       
    }



?>

The secure method could probably be upgraded quite a bit.
Anyways, this is just a base for you start off with!
__________________
Tanax is offline  
Reply With Quote