<?
// for backtracking the function calls to the page and line number.
function get_functions($trace){
    if (
count($trace) == 0)
        return 
'';
    else {
        
$array array_shift($trace);
        
$str '<strong>page:</strong> '.array_pop(explode('/'$array['file'])).' <strong>function:</strong> '.$array['function'].' <strong>line:</strong> '.$array['line'].'<br />';
        return 
get_functions($trace).$str;
    }
}

//Query function, all MySQL query's run though this,
//$var, raw query string.
//$print, output this for viewing purposes.
function query($var$print false){
global 
$mysql_query_info$config;
    if (
$config['QUERY_OUTPUT'])//if in "development" show all query's.
        
$print true;
    
$time microtime(true);//store current time for timing purposes.
    
$data mysql_query($var);// run query
    
$time round(microtime(true)-$time6);//get the time the query ran.
    
if ($time 0.5){//if the time is longer then half a second...
        
trigger_error($time.' - '.$var);//notify me thoguh a php error.
    
}
    if (
$print){//if print, store information to a structured array to be outputted at the end of file execution.
        
$rows mysql_affected_rows();
        
$str '';
        if (
mysql_errno() == && strpos($var'SELECT') === 0){// see if this a select query that has not errored and if it is run a mysql EXPLAIN to check for database optimizations.
            
$dat mysql_query('EXPLAIN '.$var);
            
$outarr = array();
            while(
$exp mysql_fetch_assoc($dat)){// for each table find, format, and save the output.
                
$str1 = array();
                
$str2 = array();
                foreach(
$exp as $key => $val){
                    
$str1[] = $key;
                    
$str2[] = $val;
                }
                if (empty(
$outarr))
                    
$outarr[] = '<td>'.implode('</td><td>'$str1).'</td>';
                
$outarr[] = '<td>'.implode('</td><td>'$str2).'</td>';
            }
            
$str '<br /><table border="2"><tr>'.implode('</tr><tr>'$outarr).'</tr></table>';
        }
        
$mysql_query_info[] = array('time' => $time'query' => $var.$str'affected' => $rows'trace' => get_functions(debug_backtrace()));
    }
    if (
mysql_errno() != 0){// if there is a MySQL error, append output to display error, and log error in database.
        
$mysql_query_info[count($mysql_query_info)-1]['query'] .= '<br />
<strong>Query failed: error# '
.mysql_errno().' -- '.mysql_error().'</strong><br />
'
.get_functions(debug_backtrace()).'<br />
'
;
        
mysql_query('INSERT INTO error (log_id, info, error_no, error) VALUES ('.$config['LOG_ID'].', \''.mysql_real_escape_string($var).'\', '.mysql_errno().', \''.mysql_real_escape_string(mysql_error()).'\')');
    }
    return 
$data;
}

function 
error_handle($code$msg$file$line$var_dump) {
global 
$error_info$config;
    
$error_info[] = $code.' : '.$msg.' : '.$file.' : '.$line;
    
query('INSERT INTO error_php (log_id, code, msg, file, line, var_dump) VALUES ('.$config['LOG_ID'].', '.$code.', \''.mysql_real_escape_string($msg).'\', \''.mysql_real_escape_string($file).'\', '.$line.', \'\')');//\''.mysql_real_escape_string(serialize($var_dump)).'\')');
}
?>