<?php

class DbConnection {
	
	var $conn;
	
	function _execQuery($query) {
		
		$resource = mysql_query($query, $this->conn);
		echo '<!-- query :: '.$query.' '
			.( $resource === false ? "\n".' - error :: ('.mysql_errno().')'.mysql_error() : '' ).'-->';
		
		return $resource;
	}
	
	function query($query) {
	
		return $this->_execQuery($query);
	}
	
	function DbConnection($address, $user, $pwd, $db_name = false) {
		
		$this->conn = mysql_connect($address, $user, $pwd, true);
		if(!$this->conn) die( "Can't connect to db. Check configurations" );
		
		if($db_name !== false ) $this->selDatabase($db_name);
	}
	
	function selDatabase($db_name) {
		
		if(!mysql_select_db($db_name, $this->conn)) 
			die( "Database not found. Check configurations" );
		
		@mysql_query("SET NAMES '".$GLOBALS['db_conn_names']."'", $this->conn);
		@mysql_query("SET CHARACTER SET '".$GLOBALS['db_conn_char_set']."'", $this->conn);
			
	}
	
	function getTableList() {
		
		$table_list = array();
		$query = "SHOW TABLES";
		$re_query = $this->_execQuery($query);
		while(list($table_list[]) = mysql_fetch_row($re_query));
		
		return $table_list;
	}
	
	function describe($table, $str = false) {
		
		if($str) $text = '';
		else $text = array();
		$query = "DESCRIBE ".$table;
		$re_query = $this->_execQuery($query);
		while($row = mysql_fetch_row($re_query)) {
			
			if($str) $text .= implode(' ', $row).'<br />';
			else $text[] = $row;
		}
		return $text;
	}
	
	function closeConn() {
		
		mysql_close($this->conn);
	}
}


?>
