<?php
	// tomekk
	// http://tomekk.oswiecim.eu.org/
	// GPL - http://www.gnu.org/copyleft/gpl.html
	// last fix: 04-10-2013

	// added: $db->seek_back() -> mysql_data_seek($result, 0)
	// removed - backup* - this function was not tested, so.... bye bye (use mysqldump, PMA instead ;-))

	class dbase
	{
		// mysql config
		var $mysql_user = 'user';
		var $mysql_pass = 'pass';
		var $mysql_host = 'localhost';
		var $mysql_db = 'database';

		// force char encoding before every query (+1 for queries counter)
		// '' to disable
		var $force_enc = 'utf8';

		// 0 - errors output with echo ""
		// 1 - errors output with die("")
		// 2 - do not show extra class errors
		var $script_stop_on_error = 1;

		////////////////////////////////////////////////////////////////////////////////////////////////
		var $handle;
		var $query;
		var $fetch;
		var $queries_counter;

		function dbase()
		{
			$this->handle = @mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_pass);

			if (!$this->handle)
			{
				$this->error(mysql_error());
			}
	
			if (!@mysql_select_db($this->mysql_db, $this->handle))
			{
				$this->error(mysql_error());
			}

			if (trim($this->force_enc) != '')
			{
				$this->query("SET NAMES $this->force_enc");
			}
		}

		function query( $query_string ) 
		{
			$this->query = @mysql_query($query_string, $this->handle);

			if (!$this->query)
			{	
				$this->error(mysql_error() . " => '" . $query_string . "'");
			}

			$this->queries_counter++;

			return $this->query;
		}

		function fetch( $query_string )
		{
			$this->fetch = @mysql_query($query_string, $this->handle);

			if (!$this->fetch)
			{
				$this->error(mysql_error() . " => '" . $query_string . "'");
			}

			$this->queries_counter++;

			$result = mysql_fetch_row($this->fetch);

			return $result[0];
		}

		function insert( $sql_table, $sql_data )
		{
			$insert_table = "INSERT INTO `" . $sql_table . "` ";
		
			$rows = '';
			$values = '';

			foreach($sql_data as $row => $value)
			{
				$rows .= "`" . $row . "`" . ", ";
				if (strtoupper($value) == 'NULL')
				{
					$values .= "NULL, ";
				} else {
					$values .= "'" . $this->resc($value) . "'" . ", ";
				}
			}

			$rows = rtrim($rows, ', ');
			$values = rtrim($values,  ', ');

			$ready_query = $insert_table . "(" . $rows . ")" . " VALUES " . "(" . $values . ")";

			if (($this->query($ready_query)))
			{
				return true;
			} else {
				return false;
			}
		}

		function update( $sql_table, $sql_data, $sql_where )
		{
			$update_table = "UPDATE `" . $sql_table . "` SET ";

			$update_string = '';

			foreach($sql_data as $row => $value)
			{
				if (strtoupper($value) == 'NULL')
				{
					$update_string .= $row . "=" . "NULL, ";
				} else {
					$update_string .= $row . "=" . "'" . $this->resc($value) . "'" . ", ";
				}
			}

			$update_string = rtrim($update_string, ', ');

			$ready_query = $update_table . $update_string . " WHERE " . $sql_where;

			if (($this->query($ready_query)))
			{
				return true;
			} else {
				return false;
			}
		}

		function resc( $string ) 
		{
			return @mysql_real_escape_string($string);
		}

		function affected_rows()
		{
			return @mysql_affected_rows($this->handle);
		}

	        function num_rows()
		{
			return @mysql_num_rows($this->query);
		}
		
		function num_fields()
		{
			return @mysql_num_fields($this->query);
		}

		function seek_back()
                {
                        return @mysql_data_seek($this->query, 0);
                }

		function queries_counter()
		{
			return $this->queries_counter;
		}

		function last_ai_id()
		{

			return @mysql_insert_id($this->handle);
		}

		function close()
		{
			if (!@mysql_close($this->handle))
			{
				$this->error(mysql_error());
			}
		}

		private function error( $err_msg )
		{
			switch($this->script_stop_on_error)
			{
				case 0:
					echo "<font color='#ff0000'>MySQL</font> - <b>" . $err_msg . "</b><br />";
					break;
				case 1:
					die ("<font color='#ff0000'>MySQL</font> - <b>" . $err_msg . "</b><br />");					
					break;
			}
		}
	}
?>
