<?php
class DbHelper {

    	public $tablePrefix;
    /**
    * Constructor
    **/
	public function __construct($prefix='')
    {
        
        if($prefix)
        	$this->tablePrefix = $prefix;
        else
       		$this->tablePrefix = MYSQL_TABLE_PREFIX;
        
    }
    public static function getResult($table,$field,$where) {
        global $tableprefix;

        $where=($where!='') ?  ' WHERE '.$where : '';

        $query = "SELECT ".$field."
    	          FROM ".$tableprefix.$table.$where ;
//echo $query;
        $res = self::execute($query);
        return self::fetchAll($res);
    }

    public static function execute($query) {
       //echo  $query;
        $res = mysql_query($query) or die(mysql_error());
        return $res;
    }

    public static function fetchAll($resultSet) {
        $resultArr = array();
        while($obj = mysql_fetch_object($resultSet)) {
            $resultArr[] = $obj;
        }
        if(empty($resultArr)) {
            return array();
        }
        return $resultArr;
    }


    public static function fetchRow($resultSet) {
        $resultArray = array();
        while($obj = mysql_fetch_object($resultSet)) {
            $resultArray[] = $obj;
        }
        if(isset($resultArray[0])) {
            return $resultArray[0];
        }
        return $resultArray;
    }

    public static function fetchOne($resultSet) {
        
      if(mysql_num_rows($resultSet)) {  
        $result = mysql_fetch_array($resultSet, MYSQL_NUM);       
      }
       return isset($result[0]) ? $result[0] : false;
    }

    public static function echopre($dataArr) {
        echo "<pre>";
        print_r($dataArr);
        echo "</pre>";
    }

    public static function getBasePath() {
        return getcwd();

    }

    public static function add_slashes($val) {
        if (get_magic_quotes_gpc()) return stripslashes($val);
        else return $val;
    }
    public static function stripChar($charArr, $string) {
        /*
        * example
        $stringArr[] = "'";
        $stringArr[] = '"';
        $stringArr[] = "-";
        */

        $stringN = str_replace($charArr, "", $string );
        return $stringN;

    } // End Function
    
    
     public static function insertData($query) {
       
        $stringN = str_replace($charArr, "", $string );
        return $stringN;

    } // End Function
    
    final public function affectedRows()
    {
        return mysql_affected_rows();
    }
	/**
	* Method to build mysql query string from an array 
	* @param array $data
	**/
    public static function buildQueryString(array $data)
    {
        $columnCount    = count($data);
        $currentColumn  = 1;
		$query = '';
        foreach ($data as $column => $value) {
            $query .= $column . ' = "' . $value.'"';
            if ($currentColumn++ <  $columnCount) {
                $query .= ', ';
            }
        }
        return $query;
    }
    
    public static function delete($table, $where,$doAudit=false)
    {
        $query = 'DELETE FROM ' . $table;
         $query .= ' WHERE ' . $where;
         //echo $query;
       self::execute($query);
        
       
        return self::hasAffected();
    }
    
     public static function hasAffected()
    {
        return mysql_affected_rows() > 0;
    }
    
    public static function update($table, array $data, $where,$doAudit=false)
    {
       $query = 'UPDATE ' . $table . ' SET ';
       $query .= self::buildQueryString($data);
       $query .= ' WHERE '.$where;
       //echo "<br>".$query;
       self::execute($query);
        
       return self::hasAffected();
    }
    
    
        
    
     public static function insert($table, array $data,$doAudit=false)
    {
        $query = 'INSERT INTO ' . $table . ' SET ';
        $query .= self:: buildQueryString($data);
       //echo "<br>".$query;
       self::execute($query);
       $insert_id = mysql_insert_id();
       
	return $insert_id;
    }
    
    
    /*Function to set table prefix for a table
   */         
        public static function setTablePrefix($tableName){
		//echo $content;exit;
            
            return MYSQL_TABLE_PREFIX.$tableName;
	}
        
     /*
Function to escape string before db operation
   */     
      public static function escapeString($data){
          return mysql_real_escape_string($data) ;
    }
    
    
    	/*
	Common function to return the row fields
	*/
 public static	function selectRecord($table,$field,$where)
    {
          
        if($where!='')
		 $where= ' WHERE '.$where;
    	$query = "SELECT ".$field."
    	          FROM ".$table.$where ;
     
               
        $res = self::execute($query);
    	return self::fetchRow($res);
    }

	/*
 	Common function to return the resultset details
	*/
 public static	function selectResult($table,$field,$where,$orderby='')
    {
		if($where!='')
			$where= ' WHERE '.$where;
    	$query = "SELECT ".$field."
    	          FROM " . $table.$where;
        
       

        if($orderby!='')
            {
                    $query.= $orderby;
            }

       // echo $query.'<br />';
        $res = self::execute($query);
    	return self::fetchAll($res);
    }

	/*
	Function to delete the  keywords of a Brands
	*/
 public static	function deleteRecord($table,$where)
	{
		$query = "DELETE FROM ".$this->tablePrefix  .$table.'  WHERE '.$where ;
                $res = self::execute($query);

	}
	
	/*
	Function to execute a query to delete a records
	*/
 public static	function customQuery($query)
	{
         $res = self::execute($query);
         return self::fetchAll($res);
	}


        // Function to select single filed 
    public static     function selectSingleValue($table,$field,$where)
    {

        if($where!='')
		 $where= ' WHERE '.$where;
    	$query = "SELECT ".$field."
    	          FROM ".$table.$where ;
	//echo $query.'<br>';

        $res = self::execute($query);
    	return self::fetchOne($res);
    }
    
    
    public static function customRecord($query)
	{
         $res = self::execute($query);
         return self::fetchRow($res);
	}


        

}
?>
