Location: PHPKode > scripts > Event calendar class > event-calendar-class/ClsConnection.cls.php
<?php
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	//									bGlobalSourcing																				//
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	//		This Page Establish connection with MySql(Database). + QueryBuilder function is for build query.						//
	//==============================================================================================================================//
	//	function ClsConnection():- 																									//
	//------------------------------------------------------------------------------------------------------------------------------//
	//	Just Change These Variables As you need	:-																					//
	//	$Host="HostName"		eg. $Host="www.bglobalsourcing.com"																	//
	//	$UserNam="UserName"		eg. $UserNam="bglobal"																				//
	//	$Paswrd="Password"		eg. $Paswrd="*******"																				//
	//	$DbName="DatabaseName"	eg. $DBName="bGlobal"																				//
	/*
		Example:-
		//For Automatic Connection According to Class Variables.
		$ObjConnection=new ClsConnection();
		
		//For User Defined Connection
		$HostName="mycompany.com";
		$UserName="aminul";
		$Password="hide@address.com";
		$DatabaseName="be_perfect";
		$ObjConnection->UserDefineConnection($HostName,$UserName,$Password,$DatabaseName);
	*/
	//==============================================================================================================================//
	//	function BuildSelectQuery($TableName,$Condition,$OrderByCol):- 																//
	//------------------------------------------------------------------------------------------------------------------------------//
	//	" $TableName "		The Name of Database Table.(Select * from $TableName)													//
	//	" $Condition "		The Condition Appended End of Query (where Filed1="something" [and Field2>40])							//
	//	" $OrderByCol "		The Order By Clause (ORDER BY Filed1[DESC])																//
	/*
		Example:-	
		$ObjDBOperation=new ClsDatabaseOperation;
		$DatabaseTableName="intra_news";
		$MessageForEmptyTable="The Table is Empty.";
		$Condition="NewsId=$NewsId";
		$OrderByCol="NewsDt";
		$CurrentNews=$ObjDBOperation->RetrieveDataFromTable($DatabaseTableName,$MessageForEmptyTable,$Condition,$OrderByCol);
		if(!is_array($CurrentNews))
			$Message=$CurrentNews;
		foreach($CurrentNews as $CurNews)	//Simply Act as Fetch_Array()
		//echo "<br>$CurNews[0]  $CurNews[1]  $CurNews[2]";
	*/
	//==============================================================================================================================//
	//	function BuildUpdateQuery($DatabaseTableName,$DatabaseTableName,$UpdateCondition=""):- 														//
	//------------------------------------------------------------------------------------------------------------------------------//
	//	" $DatabaseTableName "		The Name of Database Table.(Select * from $TableName)											//
	//	" $DatabaseTableName "		Two Dimensional Array Consists of Table Colnames with Update Value								//
	//	" $OrderByCol "		The Order By Clause (ORDER BY Filed1[DESC])																//
	/*
		Example:-
		$UpdateFieldsAndValues=array("NewsDt"=>"'$txtNewsDt'",
								"NewsShort"=>"'$txtNewsShortDesc'",
								"NewsDetail"=>"'$txtNewsDetailDesc'",
								"NewsStatus "=>"'$selStatus'",	
								"NewsImg"=>"'$BaseFileName'");	
		//UPDATE Database...
		$TableName="intra_news";																									
		$UpdateCondition="NewsId=$NewsId";					
		$CheckDuplicateCondition = "NewsShort='$txtNewsShortDesc' and NewsDt='$txtNewsDt' and NewsDetail='$txtNewsDetailDesc' and NewsId<>$NewsId";
		$ConditionalErrorMsg = "The News is Already Added.";																		
		$SuccessMessage = "News Updated Successfully.";																				
		$ErrorMessage = "Error while updating news.";																				
		$Message = $ObjUpdateData->UpdateDBTable($TableName,$UpdateFieldsAndValues,$SuccessMessage,$ErrorMessage,
														$UpdateCondition,$CheckDuplicateCondition,$ConditionalErrorMsg);
	*/			
	//==============================================================================================================================//
	//	function BuildDeleteQuery($TableName,$Condition=""):- 																		//
	//------------------------------------------------------------------------------------------------------------------------------//
	//	" $TableName "		The Name of Database Table.(Select * from $TableName)													//
	//	" $Condition "		Condition For Delete If "" Then The Table With All Content Will Deleted.								//
	/*
		Example:-
			//Build Delete Query
			$ObjConnection=new ClsConnection();
			$TableName="Student";
			$Condition="StId=2";
			$DeleteQuery=$ObjConnection->BuildDeleteQuery($TableName,$Condition="")
			echo $DeleteQuery;
	*/			
	//==============================================================================================================================//
	//	function CheckDuplicateValExist($SelQuery):- 																				//
	//------------------------------------------------------------------------------------------------------------------------------//
	//	" $SelQuery "		Select Query with Condition																				//
	//						If Found any Record then return False otherwise True.													//
	/*
		Example:-
			//Build Delete Query
			$ObjConnection=new ClsConnection();
			$SelQuery="select * from student where StId<>5";
			$InsertPermitted=$ObjConnection->CheckDuplicateValExist($SelQuery);
			echo $InsertPermitted;	//True or False.
	*/			
	//==============================================================================================================================//
	
	class ClsConnection
	{
		var $Host="localhost";
		var $UsrNam="root";
		var $Paswrd="";
		var $DBName="class_test";
		
		//Establish Database Connection While Creating or Inherit this class.
		function ClsConnection()
		{
			mysql_connect($this->Host,$this->UsrNam,$this->Paswrd)or die("Error in localhost connection.");
			mysql_select_db($this->DBName)or die("Error in Database Connections");
			//echo "Connection Established (Connection Class)";
		}

		function UserDefineConnection($HostName,$UserName,$Password,$DatabaseName)
		{
			if($HostName!="")
				$this->Host=$HostName;
			if($UserName!="")
				$this->UsrNam=$UserName;
			if($Password!="")
				$this->Paswrd=$Password;
			if($DatabaseName!="")
				$this->DBName=$DatabaseName;

			mysql_connect($this->Host,$this->UsrNam,$this->Paswrd)or die("Error in localhost connection.");
			mysql_select_db($this->DBName)or die("Error in Database Connections");
			//echo "Connection Established by user defined Data.(Connection Class)";
		}
		
		//Check Table $DatabaseTableName Existance. If Exists return True otherwise False.
		function DataBaseTableExists($DatabaseTableName)
		{
			if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$DatabaseTableName."'"))==1)
				return "True";	//Exists
			else
				return "False";	//Not Exists
		}
		
		//This Function Build " SQL SELECT " Query with A Specified Condition.
		function BuildSelectQuery($TableName,$Condition="",$OrderByCol="",$OrderAscOrDesc="asc")
		{
			if($Condition=="")
				$Query="select * from $TableName";
			else
				$Query="select * from $TableName where $Condition";
			if($OrderByCol!="")
			{
				$Query.=" ORDER BY $OrderByCol";
				if($OrderAscOrDesc!="asc" && $OrderAscOrDesc!="")
					$Query.=" ".$OrderAscOrDesc;
			}
			return $Query;
		}
	
		//This Function Build " SQL UPDATE " Query with A Specified Condition.
		function BuildUpdateQuery($DatabaseTableName,$UpdateFieldsAndValues,$UpdateCondition="")
		{
			$UpdateQuery="UPDATE $DatabaseTableName SET ";
			foreach($UpdateFieldsAndValues as $ColName=>$UpdateVal)
				$UpdateQuery.="$ColName = $UpdateVal, ";
			$UpdateQuery=substr($UpdateQuery,0,strlen($UpdateQuery)-2);
			if($UpdateCondition!="")
				$UpdateQuery.=" where $UpdateCondition";
			return $UpdateQuery;
		}
		
		function BuildDeleteQuery($TableName,$Condition="")
		{
			$Query="delete from $TableName";
			if($Condition!="")
				$Query.="where $Condition";
			return $Query;
		}
		
		function CheckDuplicateValExist($SelQuery)
		{
			//echo $SelQuery;
			$TmpQ=mysql_query($SelQuery) or die("I m dead");
			if(mysql_num_rows(mysql_query($SelQuery))==0)
				return false;
			else
				return true;
		}
		
		function SearchImageAndDelete($DatabaseTblName,$TblColNameForCondition,$TblColValForCondition,$TblColNameForImgFile,$ImgFolderLoc)
		{
			$SelQueryForDelImg=mysql_query("select $TblColNameForImgFile from $DatabaseTblName where $TblColNameForCondition=$TblColValForCondition")or die("Database Error Inside SearchImageAndDelete Function");
			$DataOfImg=mysql_fetch_row($SelQueryForDelImg);
			$ImgBaseName=$DataOfImg[0];
			$ImgWithPath=$ImgFolderLoc."/".$ImgBaseName;
			if(file_exists($ImgWithPath))
				unlink($ImgWithPath);
			else
				echo "File Not Found For Delete.";
		}
	}
?>
Return current item: Event calendar class