<?php
 /**
 * Developed by Uta Marian
 * marian@webdraft.ro
 */

class database
{
    private $conn;   
    private $stmt;
	
    public function __construct()
	{
        $this->conn = new mysqli(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        
		if(mysqli_errno($this->conn)){return false;}else{return true;}
    }
	
    public function Query()
	{
        $args = func_get_args();
        $query = $args[0];
        $params = array_slice($args,1);
        if($this->stmt = $this->conn->prepare($query)){
        	
			$this->BindParams($this->stmt,$params);
			
			$result = $this->stmt->execute();
			
			if($result){
				$fields = $this->GetColumns($this->stmt);
				
				foreach ($fields as $field){
					$bind_r[] = &${$field};
				}
				
				call_user_func_array(array($this->stmt,"bind_result"),$bind_r);
				
				$i=0;
				$results = array();
				while($this->stmt->fetch()){
					  foreach ($fields as $field) { 
							$results[$i][$field] = $$field;
					  }
					  $i++;
				}
				return $results;
				}
		}else {return false;}
		
        return false;
    }
	
	public function MultiQuery($query)
	{
		$arr = array(); 
		$i=0;
		if ($this->stmt = $this->conn->multi_query($query)) {
			do {
				if ($result = $this->conn->store_result()) {
					while($row = $result->fetch_row()){
						if($rows= $result->fetch_field()){
							$fields[$i][] = $rows->name;
							while($fetch = $result->fetch_field()){
								$fields[$i][] = $fetch->name;
							}
						}else{
							$fields[$i] = $fields[$i-1];
						}
						for($x=0; $x<count($row); $x++){
							$arr[$i][$fields[$i][$x]] = $row[$x];	
						}$i++;
					}
					$result->free();
				}
			} while ($this->conn->next_result());
		}
		return $arr;
	}
	
    public function NoQuery()
	{
    	$args = func_get_args();
        $query = $args[0];
        $params = array_slice($args,1);
        $this->stmt = $this->conn->prepare($query); 
		
		if($this->stmt){
        	$this->BindParams($this->stmt, $params);
        	return $this->stmt->execute();        	
		}
    }
	
	public function MultiNoQuery($param)
	{
		 $this->stmt = $this->conn->multi_query($param);

		  if ($this->conn->more_results()){
			while ($this->conn->next_result());
		  }
		return $this->stmt;
	}
	
	public function LastID()
	{
		return $this->conn->insert_id;
	}
	
	public function CountRows()
	{
		return $this->stmt->num_rows;
	}
	
    private function BindParams(&$stmt,&$params)
	{ 
	  $total = count($params);
      if($total > 1){
      	$arr = array(); 
		
	    foreach($params as $index => $param){
	        $arr[$index] = &$params[$index];
		}
		
        call_user_func_array(array($stmt,'bind_param'),$arr);
      }
    }
	
    private function GetColumns(&$stmt)
	{
        $arr = array();
        $result = $this->stmt->result_metadata();
        
		for($i=0; $i < $result->field_count; $i++){
        	$field = $result->fetch_field();
            $arr[] = $field->name;
        }
		$result->close();
		
		return $arr;
    } 
	
	public function __destruct()
	{
		$close = $this->conn->close();
		if($close){return true;}
	}
}