<?php
/* Connects to MySQL database and performs operations.*/
class SQL {
	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 />";
	
	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')){//local
			//$host="localhost"; $user="silvercr_auto"; $pwd="1!2@4$3#"; $db="silverstocks";
			$host="localhost"; $user="silvercr_auto"; $pwd="1!2@4$3#"; $db="automasters";
		}else{//online
			$host = "localhost"; $user = "vehicleu_user"; $pwd = "$)PXTET-nfT,"; $db = "vehicleu_db";
		}
		$this->con = mysql_connect($host,$user,$pwd);
		$this->con = mysql_select_db($db);
		if(!$this->con){ die($this->conErrorMsg); }
	}
	/* Inserts a record into the table ($tbl) using the data in the array ($arr). */
	function insertInto($tbl, $arr){
		$holeStr=""; $pegStr=""; $holepegStr="";
		foreach($arr as $name => $value){
			$name = $this->clean($name); $value = $this->clean($value); if($value=="- select -") $value = "not specified";
			$holeStr=$holeStr."{$name},";
			$pegStr=$pegStr."'{$value}',";
		}
		//strip off last comma
		$holeStr = substr($holeStr,0,(strlen($holeStr)-1)); $pegStr = substr($pegStr,0,(strlen($pegStr)-1));
		$res = mysql_query("insert into {$tbl} ({$holeStr}) values ({$pegStr})");
		
		$info = array(); if($res){ $info[0] = true; $info[1] = ""; }else{ $info[0] = true; $info[1] = mysql_error(); }
		return $info;
	}
	/* Update the table ($tbl) using the data in the array ($arr). */
	function update($tbl, $arr, $whereclause){
		$holepegStr="";
		foreach($arr as $name => $value){
			$name = $this->clean($name); $value = $this->clean($value);
			$holepegStr=$holepegStr."{$name}='{$value}',";
		}
		//strip off last comma
		$holepegStr = substr($holepegStr,0,(strlen($holepegStr)-1));
		$res = mysql_query("update {$tbl} set {$holepegStr} where {$whereclause}");
		$info = array(); if($res){ $info[0] = true; $info[1] = ""; }else{ $info[0] = true; $info[1] = mysql_error(); }
		return $info;
	}
	/* Fetches a single matching record */
	function getWhere($qry,$tbl){
		$res = mysql_query("select * from {$tbl} where {$qry} limit 1");
		$info = array(); if($res && mysql_num_rows($res)>0) $info = mysql_fetch_array($res);
		return $info;
	}
	/* Fetches all matching records */
	function fetchWhere($qry,$tbl){
		$res = mysql_query("select * from {$tbl} where {$qry}");
		$info = array(); if($res && mysql_num_rows($res)>0) $info = mysql_fetch_array($res);
		return $info;
	}
	/* Deletes the matching record(s) */
	function removeWhere($qry,$tbl){
		$res = mysql_query("delete from {$tbl} where {$qry}");
		$info = array(); if($res){ $info[0]=true; }else{ $info[0]=false; $info[1] = mysql_error(); }
		return $info;
	}
	/* Executes a general fetch query */
	function executeQuery($qry){
		$res = mysql_query($qry);
		$info = array();
		if($res){ $info[0]=true; $info[1]=$res; $info[2]=mysql_num_rows($res); }else{ $info[0]=false; $info[1] = mysql_error(); }
		return $info;
	}
	/* Executes a general update query */
	function executeUpdate($qry){
		/*$res = mysql_query("delete from {$tbl} where {$qry}");
		$info = array(); if($res){ $info[0]=true; }else{ $info[0]=false; $info[1] = mysql_error(); }
		return $info;*/
	}
	/* Fetches the next row in the resource $res */
	function nextRow($res){ return mysql_fetch_array($res); }
	
	function clean($str){ $str = mysql_real_escape_string(strip_tags($str)); return $str; }
}
?>