<?
require_function('mysql_connect');
require_function('mysql_select_db');
require_function('mysql_query');
require_function('mysql_fetch_object');
require_function('mysql_fetch_array');
require_function('mysql_fetch_row');

class mysql_db{
	// This will store the MySQL Server Information
	var $Hostname = '';
	var $Username = '';
	var $Password = '';
	var $Database = '';

	// This will store the actual resource ID
	var $conn;

	// This will setup the connection details
	function mysql_db($Hostname, $Username, $Password, $Database){
		$this->Hostname = $Hostname;
		$this->Username = $Username;
		$this->Password = $Password;
		$this->Database = $Database;
	}

	// Connect to MySQL database
	function connect(){
		$this->conn = mysql_connect($this->Hostname, $this->Username, $this->Password);
		mysql_select_db($this->Database);
	}

	// Return the MySQL Error number and text, if any
	function Error(){
		if (mysql_error($this->conn)) return mysql_errno($this->conn) . ': ' . mysql_error($this->conn);
		else return false;
	}

	// Disconnect from MySQL database (do this in footer.inc)
	function disconnect(){
		return @mysql_close($this->conn);
	}

	// See if we are connected and return boolean
	function connected(){
		if ($this->conn) return true;
		else return false;
	}

	// Query MySQL
	function Query($Query){
		$result = @mysql_query(trim($Query), $this->conn);
		return $result;
	}

	// Query MySQL and return object of first row (all columns)
	function rQuery($sql){
		$sql = $this->Query(trim($sql));
		if (!$sql) return false;

		return $this->obj($sql);
	}

	// Query MySQL and return array of first row (all columns)
	//   Warning: This function is significantly slower than rQuery()
	//   with little or no added benefit.
	function aQuery($sql){
		$sql = $this->Query(trim($sql));
		if (!$sql) return false;

		return $this->array($sql);
	}

	// Query MySQL and return array of first column (all rows)
	function lQuery($sql){
		$sql = $this->Query(trim($sql));
		if (!$sql) return false;

		while ($line = $this->row($sql))
			$lines[] = $line[0];
		return $lines;
	}

	// Query MySQL and return contents of first column in first row
	function oQuery($sql){
		$sql = $this->Query(trim($sql));
		if (!$sql) return false;

		if ($sql) 
			list($sql) = $this->row($sql);
		else
			$sql = '';

		return $sql;
	}

	// Fetch a row
	function row($sql){
		if (!$sql) return false;
		return mysql_fetch_row($sql);
	}

	// Fetch an object
	function obj($sql){
		if (!$sql) return false;
		return mysql_fetch_object($sql);
	}

	// Fetch an array
	function arr($sql){
		if (!$sql) return false;
		return mysql_fetch_assoc($sql);
	}

	// Fetch the entire table into a nested array
	function tQuery($Query){
		$sql = $this->Query($Query);
		if (!$sql) return false;

		while ($row = $this->arr($sql)){
			unset($cols);
			foreach ($row as $k=>$v)
				$cols[$k] = $v;
			$rows[] = $cols;
		}

		return $rows;
	}

	// Fetch the entire table into an array of objects
	function toQuery($Query){
		$sql = $this->Query($Query);
		if (!$sql) return false;

		while ($row = $this->arr($sql)){
			unset($cols);
			foreach ($row as $k=>$v)
				$cols->$k = $v;
			$rows[] = $cols;
		}

		return $rows;
	}
}
$db = new mysql_db($Config['Hostname'], $Config['Username'], $Config['Password'], $Config['Database']);
$db->connect();
?>
