Location: PHPKode > projects > Prsaar Social Networking Platform > PrsaarColour-0.7.alpha.6.3/pr-inside/database-connect.php
<?php	
	//Good, simple, very simple, cool, loveable class
	class DatabaseConnect		//Class to connect to database
	{
		var $Error;
		
		function DatabaseConnect()
		{
			if(!(@mysql_connect(PR_DATABASE_HOST, PR_DATABASE_USER, PR_DATABASE_PASSWORD)))
			{
				$this->ShowError('connect');
			}
			elseif(!(@mysql_select_db(PR_DATABASE_NAME)))
			{
				$this->ShowError('database');
			}
		}
		//Deleting made easy
		function Delete($Table, $Column, $Value)
		{
			$this->Result("DELETE FROM ".PR_DATABASE_PREFIX."{$Table} WHERE {$Column} = '{$Value}'");
			return true;
		}
		//Selecting made easy
		function Select($Table, $Column = false, $Value = false)
		{
			if($Column == false)
				$Result = $this->Result("SELECT * FROM ".PR_DATABASE_PREFIX."{$Table}");
			else
				$Result = $this->Result("SELECT * FROM ".PR_DATABASE_PREFIX."{$Table} WHERE {$Column} = '{$Value}'");
			return $Result;
		}
		//Run a query
		function Result($Query)
		{
			if(!($DatabaseQuery = @mysql_query($Query)))
			{
				$this->ShowError('query');
			}
			else
			{
				if(!empty($DatabaseQuery))
				{
					while($QueryResult = @mysql_fetch_array($DatabaseQuery))
					{
						$Result[] = $QueryResult;
					}
					if(isset($Result))
						return $Result;
					else
						return false;
				}
				else
				{
					return false;
				}
			}
		}
		
		function ShowError($Type)
		{
			switch($Type)
			{
				case 'connect':
					$Heading = 'Cannot connect to the database server.';
					$Content = "<a href='http://colour.prsaar.com/' >Prsaar Colour</a> is unable to connect to the database server. This may be because <ul> <li> the Username and Password given in the <code>pr-config/config.php</code> file are incorrect. Please check that you have right username and password in the <code> pr-config/config.php </code> file. </li> <li> the hostname of your database is incorrect. Please check the <code> pr-config/config.php </code> file for the hostname of your database server. </li> <li> the database server is not running. You may contact your service provider </li> </ul>";
					break;
				case 'database':
					$Heading = 'Cannot select the database';
					$Content = "<a href='http://colour.prsaar.com/'>Prsaar Colour</a> is able to connect to the database server but is unable to select the database. This may be because the database name provided in the <code>config</code> file doesnot exits";
					break;
				
				case 'query':
				default:
					$Heading = 'A database related error occured';
					$Content = '';
					break;
			}
			$ShowError = '<h2>'.$Heading.'</h2> <p> '.$Content.' </p> <p> The database returned following error- </p> <p> <code> Error '.mysql_errno().' : '.mysql_error().' </code> </p> <a href="http://colour.prsaar.com/">Prsaar Colour</a>';
			require('database-connect-showerror.php');
			exit();
		}
	}
	
	$DC = new DatabaseConnect; 	//this creates a global object for database connect
?>
Return current item: Prsaar Social Networking Platform