<?php
/*
* Connection file
*
*/
class connection {
  var $dbhost;
  var $dbname;
  var $dbpass;
  var $link;
  var $dbhost1;
  var $dbname1;
  var $dbpass1;
  var $link1;
 
  function __construct() {  	
        $this->dbhost = DB_HOST;
      $this->dbusername = DB_USERNAME;
      $this->dbname = DB_NAME;
      $this->dbpass = DB_PASS;
  }
  function select_database($link){
  	mysql_select_db($this->dbname);
  }
  function connect(){
  	$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASS, DB_NAME);
	//$this->link = mysql_connect($this->dbhost,$this->dbusername,$this->dbpass);
  	//$this->select_database($this->link);
  }
  function getLink(){
  	$this->link = mysqli_connect($this->dbhost, $this->dbusername, $this->dbpass, $this->dbname);
  	return $this->link;
  }
 public  static function execute($sql,$type='select-all'){
	
	 	$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASS, DB_NAME);
		//$rs1 = mysqli_query($link,"SET GLOBAL max_connections = 500");
	  	if($sql!=''){
			switch ($type){
				case 'select':
					$rs = mysqli_query($link,$sql);
					if($rs && mysqli_num_rows($rs) > 0){
						$returnData =  mysqli_fetch_object($rs);
					}else{
						$returnData =  false;
					}
					break;
				case 'select-all':
					$rs = mysqli_query($link,$sql);
					if($rs && mysqli_num_rows($rs) > 0){
						while ($row = mysqli_fetch_object($rs)){
							$result[] = $row;	
						}
						$returnData = $result;
					}else{
						$returnData = false;
					}
					break;
				case 'insert':
					$rs = mysqli_query($link,$sql);
					if($rs){
						$returnData = mysqli_insert_id($link);
					}else{
						$returnData = false;
					}
					break;
				case 'update': case 'delete' :
					$rs = mysqli_query($link,$sql);
					if($rs){
						$returnData = true;
					}else{
						$returnData = false;
					}
					break;
			}
	  	}
		mysqli_close($link); 
		return $returnData;
  }
   public  static function executeSingleQuery($sql){
	  	$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASS, DB_NAME);
		$rs = mysqli_query($link,$sql);
		mysqli_close($link); 
		return $rs;
	}
	 public  static function executeQuery($sql,$type='select-all'){
	
	 	$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASS, DB_NAME);
		//$rs1 = mysqli_query($link,"SET GLOBAL max_connections = 500");
	  	if($sql!=''){
			switch ($type){
				case 'select':
					$rs = mysqli_query($link,$sql);
					if($rs && mysqli_num_rows($rs) > 0){
						$returnData = mysqli_fetch_object($rs);
					}else{
						$returnData = false;
					}
					break;
				case 'select-all':
					$rs = mysqli_query($link,$sql);
					if($rs && mysqli_num_rows($rs) > 0){
						$returnData = $rs;
					}else{
						$returnData = false;
					}
					break;
				case 'insert':
					$rs = mysqli_query($link,$sql);
					if($rs){
						$returnData = mysqli_insert_id($link);
					}else{
						$returnData = false;
					}
					break;
				case 'update': case 'delete' :
					$rs = mysqli_query($link,$sql);
					if($rs){
						$returnData = true;
					}else{
						$returnData = false;
					}
					break;
			}
	  	}
		mysqli_close($link); 
		return $returnData;
		
  }
}
?>