View Single Post
Old 02-25-2009, 01:54 AM   #2 (permalink)
Enfernikus
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

I'm pretty sure I gave you this one before my 50 million file framework..

*it's old*

PHP Code:
<?php

/**
 * NOTE TO SELF
 * 
 * Database access layer for MySQL
 * internal variables are labeled as follows for easy refernece to what they contain
 *     - $iNumber  = the i at the beggining of the variable stands for an integer within the variable
 *     - $szString = the sz at the beggining of the variable represents a string within the variable
 *  - $aArray   = the a at the beggining of the variable means that there is an array within the variable
 *
 * For ease of use publicy available variables DO NOT follow this convention
*/ 

class mysql
{
    
/**
     * Set up neccesary variables, including connection and query identification as well as store connection info
    */
    
private $iConnectionID;
    private 
$iQueryID;
    public  
$num = array();
    public  
$queryCount;
    public  
$queries = array();
    
        
/**
         *  Name:__construct [ PUBLIC ]
         *  Uses: Initializes database connection
        */
        
public function __construct()
        {    
            
//now populate our connection array and make the connection
            
$this->iConnectionID mysql_connect(configVars::MYSQL_HOST,configVars::MYSQL_USER,configVars::MYSQL_PASS) or die('Unable to connect');
                                   
mysql_select_db(configVars::MYSQL_DATA,$this->iConnectionID);
        }
        
        
/**
         *  Name: query [ PUBLIC ]
         *  Uses: extracts SQL statement and injects escapes variables via sprintf
        */
        
public function query()
        {
            
//collect our arguments 
            
$iFuncArgs func_num_args();
            
$aFuncArgs func_get_args();
            
            
//lets check if we're getting a call from __call
            
if(is_array($aFuncArgs[0]))
            {
                
$aFuncArgs $aFuncArgs[0];
                
$iFuncArgs count($aFuncArgs);
            }
            
            
//check if there is only an SQL statement - if so, query it and return a record.
            
if($iFuncArgs == 1)
            {
                
$this->__query($aFuncArgs[0]);
                
$this->__populate();
                return new 
Record($this->iQueryID,$this->iConnectionID);
            }
            
            
//multiple input, break it off to secure
            
$aSql array_splice($aFuncArgs,0,1);
            
$aArgs array_map('mysql_real_escape_string',$aFuncArgs);
            
$szSql call_user_func_array('sprintf',array_merge($aSql,$aArgs));
            
$this->__query($szSql);
            
$this->__populate();
            return new 
Record($this->iQueryID,$this->iConnectionID);
        }
        
        
/**
         * Name: __call [ PUBLIC ]
         * Uses: get specified ammount of data (IE: one, row, all);
        */
        
public function __call($function,$args)
        {
            
//run our query
            
$query $this->query($args);
            
            
//now lets see what they want
            
preg_match('/get(.*?)/',$function,$matches);
            
$match next($matches);
            switch(
$match)
            {
                case 
stristr($match,'one'):
                    return @
reset($query->fields); break;
                case 
stristr($match,'row'):
                    return 
$query->fields; break;
                case 
stristr($match,'all'):
                    
$i = -1;
                    while(!
$query->EOF)
                    {
                        
$ret[++$i] = $query->fields;
                    }
                    return 
$ret; break;
                    
            }
        }
        
        
/**
         * Name: __query [ PRIVATE ]
         * Uses: MySQL wrapper for mysql_query
        */
        
private function __query($sql)
        {
            
$this->iQueryID mysql_query($sql);
            ++
$this->queryCount;
            if(
configVars::DEBUGME)
            {
                
$this->queries[$this->queryCount] = $sql;
            }
        }
        
        
/**
         * Name: __populate [ PRIVATE ]
        */
        
private function __populate()
        {
            
$this->num['fields'] = @mysql_num_fields($this->iQueryID);
            
$this->num['rows']   = @mysql_num_rows($this->iQueryID);
        }
        
}

class 
Record
{
    
/**
     * connection, query, row numbers, current row number, assoc field array, end of file mark
    */
    
private $iQueryID;
    private 
$iConnectionID;
    private 
$rows;
    public 
$curRow;
    public 
$fields;
    public 
$EOF false;
    
        
/**
         * populate iQueryID, iConnectionID, rows, and fields
        */
        
public function __construct($iQueryID,$iConnectionID)
        {
            
$this->iQueryID $iQueryID;
            
$this->iConnectionID $iConnectionID;
            
$this->rows = @mysql_num_rows($iQueryID);
            
$this->fields = @mysql_fetch_assoc($iQueryID);
        }
        
        
/**
         * Keeps track of what row we're on and stops us from erroring off
        */
        
public function movenext()
        {
            
//check to make sure we've not reached the limit
            
if($this->curRow != ($this->rows 1))
            {
                
//populate fields and increase the count
                
$this->fields mysql_fetch_assoc($this->iQueryID);
                ++
$this->curRow;
                return;
            }
            
            
//limit reached, mark of end of file
            
$this->EOF true;
        }
        
        public function 
__get($what)
        {
            return 
$this->fields[$what];
        }
}
Enfernikus is offline  
Reply With Quote