<?php
//ini_set('max_execution_time', 7200); //7200 seconds = 2 hours
/* Connects to MySQL database and performs operations. */
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="";
	
	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 = "vehicleu_user";
			$pwd = "$)PXTET-nfT,";
			$db = "vehicleu_db";
		}*/
		$host="localhost";
		$user="ops_user";
		$pwd="ops_pwd";
		$db="ops";

		
		$this->con = mysql_connect($host,$user,$pwd);
		$this->con = mysql_select_db($db);
		if(!$this->con){
			die($this->conErrorMsg);
		}
	}
	
	/* Executes a query that gets a SINGLE row of data.
	 * No errors are reported. Either a row is returned or no rows are returned for whatever reason there might be.
	*/
	function getWhere($cond, $tbl){
		$rar = array(array(), "");
		$r = mysql_query("select * from {$tbl} where {$cond} limit 1");
		if($r && mysql_num_rows($r)>0){ $rar[0] = mysql_fetch_array($r); }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	
	/* Executes a query that fetches multiple rows of data.
	 * No errors are reported. Returns the results resource object or NULL if the query failed.
	*/
	function fetchWhere($cond, $tbl){
		$rar = array(NULL, "");
		$r = mysql_query("select * from {$tbl} where {$cond}");
		if($r && mysql_num_rows($r)>0){ $rar[0] = $r; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	/* Executes a query that deletes row(s) of data.
	 * No errors are reported. Returns true for success or false if query failed.
	*/
	function deleteWhere($cond, $tbl){
		$rar = array(false, "");
		$r = mysql_query("delete from {$tbl} where {$cond}");
		if($r){ $rar[0] = true; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	/* Executes a query that counts rows of data.
	 * No errors are reported. Returns the count or zero(0) if query failed.
	*/
	function countAll($tbl){
		$rar = array(0, "");
		$r = mysql_query("select count(*) as no from {$tbl}");
		if($r && mysql_num_rows($r)>0){ $w = mysql_fetch_array($r); $rar[0] = $w['no']; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	
	/* Executes a query that counts rows of data on the given condition.
	 * No errors are reported. Returns the count or zero(0) if query failed.
	*/
	function countWhere($cond, $tbl){
		$rar = array(0, "");
		$r = mysql_query("select count(*) as no from {$tbl} where {$cond}");
		if($r && mysql_num_rows($r)>0){ $w = mysql_fetch_array($r); $rar[0] = $w['no']; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	
	/* Executes a query that does whatever the query dictates.
	 * No errors are reported. Returns the results resource object or NULL if the query failed.
	*/
	function execute($qry){
		$rar = array(NULL, "", 0, false);
		$r = mysql_query($qry);
		if($r){ $rar[0] = $r; $rar[2] = mysql_num_rows($r); if($rar[2]>0){ $rar[3] = true; } }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	/* Executes a query that fetches all rows of data.
	 * No errors are reported. Returns the results resource object or NULL if the query failed.
	*/
	function fetchAll($tbl){
		$rar = array(NULL, "");
		$r = mysql_query("select * from {$tbl}");
		if($r){ $rar[0] = $r; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	/* Points to the next row.
	*/
	function nextRow($res){ return mysql_fetch_array($res); }
	
	/* Executes a query that inserts a SINGLE row of data.
	 * Returns true if successful, false otherwise.
	*/
	function insert($loadArr, $tbl){
		$rar = array(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 = mysql_query("insert into {$tbl} ({$holeStr}) values ({$pegStr})");
		if($r && mysql_affected_rows()>0){ $rar[0] = true; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}

	function escape_string($str){
		return mysql_real_escape_string($str);
	}
	
	/* Executes a query that updates a SINGLE row of data.
	 * Returns true if successful, false otherwise.
	*/
	function updateWhere($cond, $loadArr, $tbl){
		$rar = array(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 = mysql_query("update {$tbl} set {$holePegStr} where {$cond}");
		if($r){ $rar[0] = true; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
	
	/* Executes a query that needs no feedback
	 * Returns nothing
	*/
	function simpleQuery($sql){
		return $r = mysql_query($sql);
	}

	function silentExecute($sql){
		return $r = mysql_query($sql);
	}


	/* Executes a query that needs no feedback
	 * Returns nothing
	*/
	function verifyUpdate($sql){
		$rar = array(false, "");
		$r = mysql_query($sql);
		if($r && mysql_affected_rows()>0){ $rar[0] = true; }else{ $rar[1] = mysql_error(); }
		return $rar;
	}
}
?>