<?php
//DB Connection For Search
define("DB_DB",BKDB_NAME);
define("DB_LOGIN",BKDB_USER);
define('DB_PASS',BKDB_PASS);
@mysql_connect(BKDB_HOST,DB_LOGIN, DB_PASS);

class database
{
    
    var $Database;
    var $Result;
    
    
    /*
     * Initialize
     */
    function database()
    {
        
    }
    
    
    
    /*
     * Get the ID of the record just inserted
     */
    function db_db_insert()
    {
        
        $ReturnVal = mysql_insert_id();
        
        return $ReturnVal;
        
    }
    
    
    
    /*
     * Get the number of rows of current result
     */
    function db_num_rows()
    {
        
        $ReturnVal = mysql_num_rows($this->Result);
        
        return $ReturnVal;
        
    }
    
    /*
     * Run query on database
     *	- show sql statement if failed and debug mode is ON
     */
    function db_db_query($query)
    {
        
        global $SSV_DEBUG;
        
        mysql_select_db(DB_DB) or exit(mysql_error());
        $q_result = mysql_query($query);
        
        # show sql statement if failed and debug mode is ON
        if (!$q_result && $SSV_DEBUG) {
            echo "<!--\n" . $query . "\n-->\n";
        }
        
        return $q_result;
    }
    
    
    
    /*
     * Fetch result in array
     */
    function fetchArray()
    {
        
        return mysql_fetch_array($this->Result);
        
    }
    
    
    
    /*
     * Free result after record collected
     */
    function freeResult()
    {
        
        return mysql_free_result($this->Result);
        
    }
    
    
    
    /*
     * Retrieve data from DB and return in Vector (1D array)
     */
    function returnVector($Sql)
    {
        
        $ReturnArr = array();
        
        $this->Result = $this->db_db_query($Sql);
        
        if ($this->Result && $this->db_num_rows() != 0) {
            while ($Row = $this->fetchArray()) {
                $ReturnArr[] = $Row[0];
            }
        }
        $this->freeResult;
        
        return $ReturnArr;
        
    }
    
    
    
    /*
     * Retrieve data from DB and return in 2D Array
     */
    function returnArray($Sql)
    {
        
        global $SYS_CONFIG;
        $ReturnArr = array();
        global $LibInterface, $sys_feature;
        
        $this->Result = $this->db_db_query($Sql);
        
        
        if ($this->Result && $this->db_num_rows() != 0) {
            while ($Row = $this->fetchArray()) {
                if ($sys_feature['bad_words_filter']) {
                    foreach ($Row as $Key => $Value) {
                        $Row[$Key] = badWorksFilter($Value);
                    }
                }
                $ReturnArr[] = $Row;
            }
        }
        if ($SYS_CONFIG['debug_mode']) {
            $this->freeResult;
        } else {
			mysql_free_result($this->Result);
        }
        
        return $ReturnArr;
        
    }
    
    function closeDB()
    {
        
        mysql_close();
        
    }
    
}

?>