<?php 
class db_query
{
 function getField($table,$condition,$val,$field)
{

  global $db;
   	 $sql=DB_query("select ".$field." from ".$table." where ".$condition." ='".$val."'",$db);
	 $r=DB_fetch_array($sql);
	//echo "select ".$field." from ".$table." where ".$condition." ='".$val."'"; 
   	return ($r[$field]);
}
 function GetFieldValue($table,$condition ,$field)
{

  global $db;
//echo "select ".$field." from ".$table." where ".$condition." ='".$val."'";
   	 $sql=DB_query("select ".$field." from ".$table." where  $condition  ",$db);
	 $r=DB_fetch_array($sql);
   	return ($r[$field]);
}

function getFieldByCompanyId($table,$condition,$val,$field, $comp)
{
   global $db;
   	 $sql=DB_query("select ".$field." from ".$table." where $condition ='".$val."' and   $comp",$db);
	 $r=DB_fetch_array($sql);
   	return ($r[$field]);
}
function RecordInsert($table, $dataArray)
{ 
 	global $db;
  	  $sql="Insert into ".$table ." set ";
 		$i=0;
 		foreach($dataArray as $key => $val)
 			{
 			if($i==0)
			$sql.=" ".$key." = '".$val."'";
			else
			$sql.=", ".$key." = '".$val."'";			
			$i++;
			}
	
	//  echo "<br>*************INSERT***************************************************</br>"			;
	//  echo $sql;

	 $r=DB_query($sql,$db);
	return ($r); 
} 

function RecordEdit($table, $dataArray, $cond)
 		{
		global $db;
 		 $sql="update  ".$table ." set ";
 		$i=0;
 		foreach($dataArray as $key => $val)
 			{
 			if($i==0)
 			$sql.=" ".$key." = '".$val."'";
 			else
 			$sql.=",".$key." = '".$val."'";			
 			$i++;
 			}
			$sql .=" where  $cond" ;

 //echo "<br>******************************************************************</br>"			;
//  echo $sql;		 
 	 $r=DB_query($sql,$db);
	return ($r); 
 	} 
 
 
   function GetRecordById($table_name,  $condition)
 	{   //echo "SELECT * FROM $table_name where  $condition ";
		global $db;
  	   $sql=Db_query("SELECT * FROM $table_name where  $condition ",$db) ;
	   return $sql;
	   
	}
	
 
 //***********************************************************************
 	function insertRecord($table, $dataArray)
 		{
  	  $sql="Insert into ".$table ." set ";
 		$i=0;
 		foreach($dataArray as $key => $val)
 			{
 			if($i==0)
			$sql.=" ".$key." = '".$val."'";
			else
			$sql.=", ".$key." = '".$val."'";			
			$i++;
			}
	//   echo "<br>*************INSERT***************************************************</br>";
	//   echo $sql;
 			return ($sql); 
 	} 
 
 // Function to edit record in Database 
 	function EditRecord($table, $dataArray, $cond)
 		{
 		 $sql="update  ".$table ." set ";
 		$i=0;
 		foreach($dataArray as $key => $val)
 			{
 			if($i==0)
 			$sql.=" ".$key." = '".$val."'";
 			else
 			$sql.=",".$key." = '".$val."'";			
 			$i++;
 			}
			$sql .=" where  $cond" ;

// echo "<br>******************************************************************</br>"			;		 
// echo  $sql;
		return ($sql); 
 	} 

 function DisplayRecord($table_name,  $condition)
 	{
		global $db;
   	     $sql=DB_query("SELECT * FROM $table_name where  $condition " ,$db);
  		return ($sql); 
 	}
	
	
 function GetLastRecord($table_name,  $condition,$field)
 	{
		global $db;
   	     $sql=DB_query("SELECT $field FROM $table_name where  $condition  order by $field desc limit 1" ,$db);
		 $r=DB_fetch_array($sql);
  		 return ($r[$field]);
 	}
	
	
   function GetRecord($table_name,  $condition)
 	{
  	   $sql="SELECT * FROM $table_name where  $condition " ;
		//echo $sql;
		return $sql;

	}
	
	function DeleteRecord($table_name,  $condition)
 	{
  		global $db;
  	    $sql=DB_query("delete FROM $table_name where  $condition ",$db) ;
 		return $sql;
	}

	function pageLocation($filename)
 	{
 		echo "<SCRIPT LANGUAGE='JavaScript'>window.location='$filename'</SCRIPT>"; 
 	}
	
function FinedDays($fromdate,$todate){
  $diff=abs(strtotime($fromdate) - strtotime($todate));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
return  $days+1;
 
}	
	
}
$fnLib=new db_query();
$s=new db_query();
	?>