<?php
/* This class will be used for common data base operations such as 
 connect, add, modify, delete, find data and database
*/

	class dbopenconn
	{
		var $connection;
		var $db;
		var $userid;
		// constructor
		function __construct()
		{
			//$connect = @mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("could not connect to server 1");
			// Selecting the Database for use 
			//$this->connection = $connect;
			//$db_select = @mysql_select_db(DB_NAME) or die("could not select the database in dbopenconn 1"); 
			//$this->db = $db_select;
		}
		function dbconnect()
		{
			//global $G_employeeid;
			//$G_employeeid = 11111;
			//$this-> userid = $G_employeeid;
						
			$connect = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die("could not connect to server");
			if (!$connect) {
    			echo "Error: Unable to connect to MySQL." . PHP_EOL;
    			echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    			echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    			exit;
			}					
			$this->connection = $connect;
			// Selecting the Database for use 
		
			$db_select = mysqli_select_db($connect,DB_NAME) or die("could not select the database"); 
			$this->db = $db_select;
		}
		function dbdisconnect()
		{
			mysqli_close($this->connection);
		}
		function select_query($sqltext,$print="")
		{
			$query = $sqltext;
			$result = ($print == "")?mysqli_query($this->connection,$query):$query;
			return $result;	
		}
		function insert_query($sqltext,$print="")
		{
			$query = $sqltext;
			$result = ($print == "")?mysqli_query($this->connection,$query):$query;
		    $last_id = mysqli_insert_id($this->connection);
			return $last_id;
// 			return $result;
		}
		function update_query($sqltext,$print="")
		{
			$query = $sqltext;
			$result = ($print == "")?mysqli_query($this->connection,$query):$query;
			return $result;
		}
		function delete_query($sqltext,$print="")
		{
			$query = $sqltext;
			$result = ($print == "")?mysqli_query($this->connection,$query):$query;
			return $result;
		}
		function numRows($result)
		{
			$rows = $result->num_rows;
			if ($rows === null) 
			{
				return $this->mysqlRaiseError();
			}
			return $rows;
		}
		function mysqlRaiseError($errno = null)
		{
			
		}
		function lookupdata_add($got_array)
		{
			$date = date("Y-m-d : h:i:sa");
			$got_array['entered_by'] = $this-> userid;
			$got_array['entry_date'] = $date;
			
			return $got_array;		}		
		function lookupdata_edit($got_array)
		{
			$date = date("Y-m-d : h:i:sa");
			$got_array['last_edited_by'] = $this-> userid;
			$got_array['last_edit_date'] = $date;
			
			return $got_array;		}
		
	
	}
	
/*	class CommonProc
	{
		function __construct()
		{
			$this-> obj1 = new dbopenconn();
			$this-> con = $this->obj1-> dbconnect();
		}
		
		// get list of or single compani(es) from database
		function getResults($ID='',$sql='',$debugquery='',$fields='*',$tablename='',$KeyName='')
		{
			if ($ID!="")
			{
				$sqltext = "Select $fields from $tablename where $KeyName ='".$ID."'";
			}
			else if ($sql!="")
			{
				$sqltext = $sql;
			}
				
			$companies = $this->obj1->select_query($sqltext);
			$compstr = array();
			if ($companies)
			{
				$numrows = $this->obj1->numRows($companies);
			}
			if ($numrows ==1)
			{
				while ($companylist = mysqli_fetch_assoc($companies))
				{
					$compstr[] = $companylist;
				}	
			}
			else if($numrows > 1)
			{
				while ($companylist = mysqli_fetch_assoc($companies))
				{
					$compstr[] = $companylist;
				}
			}
			//$this->obj1->dbdisconnect();
			return $compstr;
		
		}
		// add company to database
		function AddToTable($CompanyData,$TableName)
		{
		if (is_array($CompanyData)== false)
			{
				return false;
			}
			else
			{
				$fieldnames =  "(";
				$fieldvalues = "(";
				foreach ($CompanyData as $k => $v) 
				{
					
					$fieldnames .=  ($fieldnames=="(")?"$k":",$k";
					$fieldvalues .= ($fieldvalues=="(")?"'$v'":",'$v'";
				}
				$sqltext = "insert into $TableName ".$fieldnames.") values ".$fieldvalues.")";
				//echo $sqltext;
				$applications = $this->obj1->insert_query($sqltext);
				
				return ($applications == 1)?"true":"false";
			}
		
		}
		function EditToTable($applicationdata,$WhereCondition,$TableName)
		{
			if (is_array($applicationdata)== false)
			{
				return false;
			}
			else
			{
				$fieldnamesvalues =  "set ";
				
				foreach ($applicationdata as $k => $v) 
				{
					$fieldnamesvalues .=  ($fieldnamesvalues =="set ")?"$k='$v'":",$k='$v'";
				}
				$sqltext = "update $TableName ".$fieldnamesvalues." where ".$WhereCondition ;
				//echo $sqltext;
				$applications = $this->obj1->update_query($sqltext);
				return ($applications == 1)?"true":"false";
			}
		}
		
		function DeleteFromTable($applicationdata,$KeyName,$TableName,$sql='')
		{
			if(($applicationdata!='')&&($KeyName!='')&&($TableName!=''))
			{
				$sqltext = "delete from $TableName where $KeyName ='".$applicationdata."'";
				$applications = $this->obj1->delete_query($sqltext);
				$this->obj1->dbdisconnect();
				return ($applications == 1)?"true":"false";
			}
			else if ($sql!=''){
				$applications = $this->obj1->delete_query($sql);
				$this->obj1->dbdisconnect();
				return ($applications == 1)?"true":"false";
			} 
		
		}
		function disconnect_db()
		{
			$this->obj1->dbdisconnect();
		}
	}*/
