<?php
/********* MySQL Class ********/

// Author: slave@codegrunt.com / http://codegrunt.com
// database object holds connection details and performs queries
// places associative array of all rows in $this->results
// expects _db_server,_db_pass,_db_user to be defined in advance
//
// USAGE:  
//
// $db=new MySQL;
// $db->select_db(_db_name);

define('_db_server','example.com');
define('_db_user','janedoe');
define('_db_pass','riaa suck');
define('_db_name','janedoe_db');

class 
MySQL
{
    var 
$db;
    var 
$err;
    var 
$result;

    function 
MySQL()
    {
        
// initiate database connection
        
$this->err='';
        
$this->db=NULL;
        if(
defined('_db_server') && defined('_db_user') && defined('_db_pass'))
        {
            if(! 
$this->db mysql_connect(_db_server,_db_user,_db_pass))
            {
                
$this->err='could not connect to MySQL server';
                if(
defined('_debug_sql'))
                {
                    die(
$this->err);
                }
            }
        }
        else
        {
            
$this->err='insufficient details available to make database connection';
            if(
defined('_debug_sql'))
            {
                die(
$this->err);
            }
        }
    }

    function 
select_db($database)
    {
        if (! 
mysql_select_db(addslashes($database),$this->db))
        {
            
$this->err='could not select database '.$database;
            {
                die(
$this->err);
            }
        }
        else
        {
            
$this->err='';
        }
    }

    function 
affected_rows()
    {
        return(
mysql_affected_rows($this->db));
    }

    function 
do_query($query)
    {
        if(
$this->db)
        {
            
$this->result=array();
            
$result mysql_query($query,$this->db);
            
$this->err mysql_error();
            if(
strlen($this->err)==0)
            {
                
$x=0;
                
$this->result[0]['numrows']=@mysql_num_rows($result);
                
$this->result[0]['insert_id']=mysql_insert_id();
                if(
$this->result[0]['numrows']!=0)
                {
                    while(
$dbrow=mysql_fetch_array($result,MYSQL_ASSOC))
                    {
                        
$y=count($dbrow);
                        
reset($dbrow);
                        for(
$y=count($dbrow);$y>0;$y--)
                        {
                            
$this->result[$x][key($dbrow)]=current($dbrow);
                            
next($dbrow);
                        }
                        
$x++;
                    }
                }
                if(
$result) { @mysql_free_result($result); }
            }
            else if(
defined('_debug_sql'))
            {
                {
                    die(
'<p>'.$this->err.'</p><blockquote>'.$query.'</blockquote>');
                }
            }
        }
        else
        {
            
$this->err='no database connection';
            {
                die(
$this->err);
            }
        }
    }

    
// grab array of column values from result array
    
function get_attributes(&$result,$col='id')
    {
        
$attr=array();
        if(
is_array($result))
        {
            if(
count($result)>0)
            {
                foreach(
$result AS $v)
                {
                    
array_push($attr,$v[$col]);
                }
            }
        }
        return 
$attr;
    }

    function 
get_one_attribute(&$result,$key=0,$src_col='id',$dest_col='')
    {
        if(
is_array($result))
        {
            if(
count($result)>0)
            {
                foreach(
$result AS $v)
                {
                    if(
$v[$src_col]===$key)
                    {
                        return 
$v[$dest_col];
                    }
                }
            }
        }
        return 
FALSE;
    }

    
// copy result array to object using associative array for attribute names
    
function row2obj(&$a,&$obj)
    {
        foreach(
$a AS $k=>$v)
        {
            if(
$k==='numrows' || $k==='insert_id') continue;

            if(
is_numeric($v))
            {
                if(
is_int($v))
                {
                    
$obj->{$k}=(int)$v;
                }
                else
                {
                    
$obj->{$k}=(float)$v;
                }
            }
            else
            {
                
$obj->{$k}=$v;
            }
        }
    }
}

?>