Location: PHPKode > scripts > MySQL Database > mysql-database/MYSQL_DATABASE.php
<?php

require_once('DESTRUCTOR.php');

/**
 * Controls all functionality for database communications.
 * Copyright (C) 2005 - Bryan Smith
 */
class MYSQL_DATABASE extends DESTRUCTOR
{
	/**
	 * MySQL Database Object
	 *
	 * @var Object
	 */
	var $DataBase_;
	/**
	 * SQL Query provided by user
	 *
	 * @var String
	 */
	var $Query_;
	/**
	 * Entire Result Set
	 *
	 * @var Array
	 */
	var $Results_ = array();
	
	/**
	 * Creates connection to database
	 *
	 * @param String $Server
	 * @param String $Name
	 * @param String $User
	 * @param String $Password
	 * @return DATABASE
	 */
	function MYSQL_DATABASE($Server = 'localhost', $Name = 'root', $User = 'sa', $Password)
	{
		// Must call for destructor to work.
		parent::DESTRUCTOR();
		
		$this->DataBase_ = mysql_connect($Server, $User, $Password);
		if (!mysql_select_db($Name, $this->DataBase_))
			die(mysql_error());
	}
	
	/**
	 * Executes query and builds array of repsonse.
	 *
	 * @param String $SQL
	 * @param Int $Results
	 */
	function Query($SQL, $Results = MYSQL_ASSOC)
	{
		// Clear out old array before populating new results.
		$this->Query_ = null;
		$this->Query_ = mysql_query($SQL);
		$this->GetResults_($Results);
		mysql_free_result($this->Query_);
	}
	
	/**
	 * Retrieves a single result record from the MySQL response.
	 *
	 * @param Int $Results
	 * @return Array
	 */
	function GetResult($Results = null)
	{
		return mysql_fetch_array($this->Query_, $Results);
	}
	
	/**
	 * Builds an array of all rows from the MySQL response.
	 *
	 * @param unknown_type $Results
	 */
	function GetResults_($Results = null)
	{
		if (!$this->Query_)
			die (mysql_error());
		
		while ($ThisRow = $this->GetResult($Results))
			$this->Results_[] = $ThisRow;
	}
	
	/**
	 * Returns an entire record based on array index.
	 *
	 * @param Int $Index
	 * @return Object
	 */
	function GetRecord($Index)
	{
		return new DATABASE_RECORD($Index, $this);
	}
	
	/**
	 * Retrives a value specified by array index and associative key.
	 *
	 * @param Int $Index
	 * @param String $Key
	 * @param String $Default
	 * @return String
	 */
	function GetField($Index, $Key, $Default = null)
	{
		if (!empty($this->Results_[$Index][$Key]))
			return trim($this->Results_[$Index][$Key]);
		elseif (!empty($this->Results_[0][$Key]))
			return trim($this->Results_[0][$Key]);
		else
			return $Default;
	}
	
	/**
	 * Close MySQL data connection.
	 *
	 */
	function __destructor()
	{
		mysql_close($this->DataBase_);
		echo 'HEY!';
	}
}

/**
 * Retrieves entire row from DATABASE
 *
 */
class DATABASE_RECORD
{
	var $Index_;
	var $Data_;
	
	/**
	 * Assigns DATABASE reference to current object.
	 *
	 * @param Int $Index
	 * @param Object $Data
	 * @return DATABASE_RECORD
	 */
	function DATABASE_RECORD($Index, &$Data)
	{
		$this->Index_ = $Index;
		$this->Data_ = $Data;
	}
	
	/**
	 * Retrives a single field by key.
	 *
	 * @param String $Key
	 * @param String $Default
	 * @return String
	 */
	function GetField($Key, $Default = null)
	{
		return $this->Data_->GetField($this->Index_, $Key, $Default);
	}
}

$DataBase = new MQSL_DATABASE(DB_SERVER, DB_NAME, DB_USER, DB_PASSWORD);
?>
Return current item: MySQL Database