<?php

class Database
{
    function __construct()
    {
        require('config.php');

        if( !mysql_connect( $db_host, $db_user, $db_password ) )
        {
            die("Couldn't connect to database server '$db_host' as user '$db_user'\n" .
                "Reason: " . mysql_error() );
        }
        
        @mysql_select_db( $db_name )
        or die('Unable to select database: ' . $db_name . ', reason: ' . mysql_error() );        
    }
    
    function __destruct()
    {
        mysql_close();
    }
    
    function sql_query($query)
    {
        //echo "<p>$query</p>";
        $result = mysql_query($query);
        if(!$result) echo( '<p>' . mysql_error() . "</p>\n");
        return $result;
    }
    
    function sql_fetchrow($result)
    {
        return mysql_fetch_array($result,MYSQL_ASSOC);
    }
    
    function sql_freeresult($result)
    {
        mysql_freeresult($result);
    }
    
    function clean_string($string)
    {
        return mysql_real_escape_string($string);
    }
    
    function last_insert_id()
    {
        return mysql_insert_id();
    }
};

global $db;
if(!isset($db) || empty($db)) $db = new Database();

?>
