<?php
class DBConnection {
	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  = "vehicleduty_us";
        	$pwd  = "T21a@FTOC2n#";
        	$db = "vehicleduty_ops";
		}else{
			$host = "localhost";
			$user = "ops_user";
			$pwd = "ops_pwd";
			$db = "ops";
		}*/
		$host = "localhost";
		$user  = "vehicleduty_us";
        $pwd  = "T21a@FTOC2n#";
        $db = "vehicleduty_ops";
		
		$this->con = mysqli_connect($host,$user,$pwd,$db);
		if(!$this->con){
			echo "<style>html,body{ color:#FFFFFF; font-family:monospace; font-size:15px; }</style>";
			die($this->conErrorMsg);
		}
	}

	function escape_string($str){
		return mysqli_real_escape_string($this->con, $str);
	}

	function simpleQuery($sql){
		return $r = mysqli_query($this->con, $sql);
	}
	
	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);
		return mysqli_query($this->con,$qry);
	}
	
	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;
	}
}