<?php
class DBCon {
	public $con;
	public $conErrorMsg = "<br /><br /><div align='center'>We are sorry ...<hr /><h2>AN UNUSUAL ERROR OCCURED</h2><i>The Application Was Shut Down!</i></div><br /><br /><hr />";
	public $dbname="";
	public $errormsg = "";
	
	function __construct(){
		// Find out the connection parameters to use, whether for local database or hosting space
		if(stristr($_SERVER['HTTP_HOST'], 'local') || (substr($_SERVER['HTTP_HOST'], 0, 7) == '192.168')){
			$host="localhost";
			$user="root";
			$pwd="";
			$db="avvs";
		}else{
			$host = "localhost";
			$user = "sdatacaller";
			$pwd = "pW4#dUz@oV@";
			$db = "sdatastore";
		}
		
		$this->con = mysqli_connect($host,$user,$pwd,$db);
		if(!$this->con){
			die($this->conErrorMsg);
		}
		/*mysqli_query($this->con,'SET GLOBAL log_output = "FILE"');
		mysqli_query($this->con,'SET GLOBAL general_log = "ON"') ;
		mysqli_query($this->con,'SET GLOBAL general_log_file = "C:/Users/Nathaniel/Desktop/querylog.txt"');*/

	}
	
	function getWhere($cond, $tbl){
		$ret = array();
		$r = mysqli_query($this->con,"select * from {$tbl} where {$cond} limit 1");
		if($r && mysqli_num_rows($r)>0){ $ret = mysqli_fetch_array($r,MYSQLI_ASSOC); }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function fetchWhere($cond, $tbl){
		$ret = NULL;
		$r = mysqli_query($this->con,"select * from {$tbl} where {$cond}");
		if($r && mysqli_num_rows($r)>0){ $ret = $r; }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function deleteWhere($cond, $tbl){
		$ret = false;
		$r = mysqli_query($this->con,"delete from {$tbl} where {$cond}");
		if($r){ $ret = true; }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function countAll($tbl){
		$ret = 0;
		$r = mysqli_query($this->con,"select count(*) as no from {$tbl}");
		if($r && mysqli_num_rows($r)>0){ $w = mysqli_fetch_array($r,MYSQLI_ASSOC); $ret = $w['no']; }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function countWhere($cond, $tbl){
		$ret = 0;
		$r = mysqli_query($this->con,"select count(*) as no from {$tbl} where {$cond}");
		if($r && mysqli_num_rows($r)>0){ $w = mysqli_fetch_array($r,MYSQLI_ASSOC); $ret = $w['no']; }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function execute($qry){
		$ret = array(NULL, 0, false);
		$r = mysqli_query($this->con,$qry);
		if($r){ $ret[0] = $r; $ret[1] = mysqli_num_rows($r); if($ret[1]>0) $ret[2] = true; }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function executeUpdate($qry){
		$ret = array(NULL, 0, false);
		$r = mysqli_query($this->con,$qry);
		if($r){ $ret[0] = $r; $ret[1] = mysqli_affected_rows($this->con); if($ret[1]>0) $ret[2] = true; }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function silentExecute($qry){
		$ret = array(NULL, 0, false);
		$r = mysqli_query($this->con,$qry);
		if($r){ $ret[0] = $r; }
		return $ret;
	}
	
	function fetchAll($tbl){
		$ret = array(NULL, 0, false);
		$r = mysqli_query($this->con,"select * from {$tbl}");
		if($r){ $ret[0] = $r; $ret[1] = mysqli_num_rows($r); if($ret[1]>0){ $ret[2] = true; } }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function getNumRows($res){ return mysqli_num_rows($res); }
	
	function nextRow($res){ return mysqli_fetch_array($res,MYSQLI_ASSOC); }
	
	function insert($loadArr, $tbl){
		$ret = array(NULL, 0, false);
		$holeStr =''; $pegStr = '';
		//get hole and peg strings
		foreach($loadArr as $field => $value){ $holeStr = $holeStr."{$field},"; $pegStr = $pegStr."'{$value}',"; }
		//strip off last comma
		$holeStr = substr($holeStr,0,(strlen($holeStr)-1));
		$pegStr = substr($pegStr,0,(strlen($pegStr)-1));
		
		$r = mysqli_query($this->con,"insert into {$tbl} ({$holeStr}) values ({$pegStr})");
		if($r){ $ret[0] = $r; $ret[1] = mysqli_affected_rows($this->con); if($ret[1]>0){ $ret[2] = true; } }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
	
	function updateWhere($cond, $loadArr, $tbl){
		$ret = array(NULL, 0, false);
		$holePegStr='';
		//get holepeg string
		foreach($loadArr as $field => $value){ $holePegStr = $holePegStr."{$field}='{$value}',"; }
		//strip off last comma
		$holePegStr = substr($holePegStr,0,(strlen($holePegStr)-1));
		
		$r = mysqli_query($this->con,"update {$tbl} set {$holePegStr} where {$cond}");
		if($r){ $ret[0] = $r; $ret[1] = mysqli_affected_rows($this->con); if($ret[1]>0){ $ret[2] = true; } }else{ $this->errormsg = mysqli_error($this->con); }
		return $ret;
	}
}