<?php

class MySQL
{
	var $show_errors	= true;

    var $host 			= '';
    var $username    	= '';
    var $password		= '';
    var $database		= '';

    var $conn_id		= 0;

    function MySQL( $host, $username, $password, $database )
    {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
        
        $this->connect();
    }

    function connect()
    {
    	$this->conn_id = mysql_connect( $this->host, $this->username, $this->password );

        if ( !$this->conn_id )
        {
            $this->_error( 'Cannot connect to MySQL Server.' );
        }
        else
        {
            $this->select_db( $this->database );
        }
    }

    function select_db( $database )
    {
        if( !@mysql_select_db( $database, $this->conn_id ) )
        {
            $this->_error( 'Cannot select Database "'.$database.'"' );
        }
        else
        {
            $this->database = $database;
        }
    }

    function query( $query_string )
    {
        $query_result = mysql_query( $query_string, $this->conn_id );

        if ( !$query_result )
        {
            $this->_error('Invalid Query: '.$query_string );
        }

        return $query_result;
    }

    function fetch_array( $query_result )
    {
        if( func_num_args() == 1 )
        	$record = mysql_fetch_array( $query_result );
        else
        {
            $arg = func_get_arg( 1 );
            $record = mysql_fetch_array( $query_result, $arg );
        }

        return $record;
    }

    function fetch_complete( $query_string, $assoc = false )
    {
    	$query_result = $this->query( $query_string );
        $final_array = array();
		
     	if($assoc === true) {
     		while( $row = $this->fetch_array( $query_result ) ) {
        		$final_array[$row[0]] = $row;
     		}
     	} else {
     		while( $row = $this->fetch_array( $query_result ) ) {
        		$final_array[] = $row;
     		}
     	}
        return $final_array;
    }

    function free_result( $query_result )
    {
        return @mysql_free_result( $query_result );
    }

    function query_first( $query_string )
    {
        $query_result = mysql_query( $query_string, $this->conn_id );

        if ( !$query_result )
        {
            $this->_error('Invalid Query: '.$query_string );
        }

        if( func_num_args() == 1 )
        	$return_array = $this->fetch_array( $query_result );
        else
        {
        	$arg = func_get_arg( 1 );
        	$return_array = $this->fetch_array( $query_result, $arg );
        }

        $this->free_result( $query_result );

        return $return_array;
    }

    function num_rows( $query_result )
    {
        return mysql_num_rows( $query_result );
    }

    function insert_id()
    {
        return mysql_insert_id( $this->conn_id );
    }

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

    function _error( $error_msg )
    {
	    $error_msg	 = "<b>Database error:</b> ".$error_msg."\n<br />";
	    $error_msg	.= "<b>mysql error:</b> ".mysql_error()."\n<br />";
	    $error_msg	.= "<b>mysql error number:</b> ".mysql_errno()."\n<br />";
	    $error_msg	.= "<b>Script:</b> ".getenv( "REQUEST_URI" )."\n<br />";
	    $error_msg	.= "<b>Referer:</b> ".getenv( "HTTP_REFERER" )."\n<br /><br />";

	    if( !$this->show_errors )
        {
        	$errormsg = "<!--\n".$error_msg."\n-->";
        }

        die( "</table>\n<font face=\"Verdana,Arial,Helvetica\" size=2>\n<b>SQL-DATABASE ERROR</b>\n<br /><br />\n".$error_msg."\n</font>" );
    }
}

?>