<?
class mysql_controller
{//DigiHex Systems\\


	function init() {
		$this->connect();
	}


	function connect() {
		global $CONF;

		try {
			$this->CONNECTION=mysql_connect($CONF->SQL_DB_HOST, $CONF->SQL_DB_USER, $CONF->SQL_DB_PASS);
			if(!mysql_select_db($CONF->SQL_DB_NAME, $this->CONNECTION)) throw new Exception('mysql_error');
		} catch (Exception $e) {
			$mysql_error=mysql_error();
			$err=$e->getTrace();
			$error=$err[1];
			$error_string='<b>'.$mysql_error.'</b><br /><br />ERROR: mysql_select_db()';
			$error_message='<b>Code-Trace:</b><br />'.nl2br($e->getTraceAsString());

			error_handler(4096, $error_string, $error['file'], $error['line'], $error_message);
		}

		$this->query(' SET NAMES utf8 ');
	}
	function disconnect() { mysql_close($this->CONNECTION); }


	function q($query, $silent_error=false) { return $this->query($query,$silent_error); }
	function query($query,$silent_error=false) {
		try {
			$R=mysql_query($query, $this->CONNECTION);
			if($R==false) {
				if($silent_error==true) {
					return 'error';
				} else {
					throw new Exception('mysql_error');
				}
			}
		} catch (Exception $e) {
			$mysql_error=mysql_error();
			if(stristr($mysql_error, 'marked as crashed')) $this->repair_tables();
			$err=$e->getTrace();
			$error=$err[1];
			$error_string='<pre>'.str_replace('			',' ',$query).'</pre><br /><br /><b>'.$mysql_error.'</b>';
			$error_message='<b>Code-Trace:</b><br />'.nl2br($e->getTraceAsString());

			error_handler(4096, $error_string, $error['file'], $error['line'], $error_message);
		}
		return $R;
	}


	function fetch_array($sql) 	{ return @mysql_fetch_array($sql); }
	function far($sql) 				{ return $this->fetch_array($sql); }
	function fetch_assoc($sql) 	{ return @mysql_fetch_assoc($sql); }
	function fa($sql) 				{ return $this->fetch_assoc($sql); }
	function num_rows($sql) 		{ return @mysql_num_rows($sql); }
	function nr($sql) 				{ return $this->num_rows($sql); }
	function num_cols($sql) 		{ return @mysql_num_fields($sql); }
	function nc($sql) 				{ return $this->num_cols($sql); }
	function affected_rows()		{ return @mysql_affected_rows(); }
	function ar()						{ return $this->affected_rows(); }
	function inserted_id($table) 	{ return $this->fast(' SELECT id FROM '.$table.' ORDER BY id DESC LIMIT 1 '); }
	function ii($table) 				{ return $this->inserted_id($table); }


	function single($query, $silent_error=false) { return $this->fast($query,$silent_error); }
	function f($query, $silent_error=false) { return $this->fast($query,$silent_error); }
	function fast($query, $silent_error=false) {
		$sql=$this->query($query,$silent_error);
		if($silent_error==true && $sql=='error') return $sql;
		$dane=$this->fetch_array($sql);
		return ($this->num_cols($sql)==1 ? $dane[0] : $dane);
	}


	function list_cols($table, $full=false) {
		$sql=$this->query(' SHOW COLUMNS FROM '.$table.' ');
		if($this->num_rows($sql)==0) return array();
		while($dane=$this->fetch_array($sql)) {
			$cols[]=($full==true ? $dane : $dane['Field']);
		}
		return $cols;
	}


	function get_array($query, $assoc=false) {
		$sql=$this->query($query);
		if($this->num_rows($sql)==0) return ($assoc==false ? false : array());
		while($dane=$this->far($sql)) {
			if($assoc==false) {
				$array_elem[]=$dane[0];
			} else {
				$array_elem[$dane[0]]=$dane[1];
			}
		}
		return $array_elem;
	}


}//DigiHex Systems\\
?>