<?php
/********************************
** MYSQL knjižnica				 **
** Author: Dave @ zabec.net	 **
** Date: 2007 						 **
********************************/

/* MYSQL class, uporablja errorHandling class */
/* class skrbi za povezavo z MYSQL databazo   */
/**********************************************/
class Mysql
{
	var $host;
	var $dbUser;
	var $dbPass;
	var $dbConn;
	var $dbName;
	var $charset;

	//constructor
	function Mysql($host, $dbUser, $dbPass, $dbName, $charset = "utf8", $noerror = false)
	{
		global $debug;
		global $settings;
		global $mod_path;
		global $logmode;
		$this->host = $host;
		$this->dbUser = $dbUser;
		$this->dbPass = $dbPass;
		$this->dbName = $dbName;
		$this->charset = $charset;
		$this->logmode = $logmode;
		$this->noerror = $noerror;

		if($this->noerror)
		{
			//errorHandling class
			$this->error = new errorHandling($debug, "db", $settings['template'], $mod_path, $this);
		}

		$this->connectToDb();
	}

	//povezava v databazo
	function connectToDb()
	{
		if (!$this->dbConn = @mysql_connect($this->host, $this->dbUser, $this->dbPass))
		{
			echo "Napaka pri povezavi v databazo!";
			exit;
		}
		elseif (!@mysql_select_db($this->dbName, $this->dbConn))
		{
			echo "Napaka pri izbiri databaze!";
			exit;
		}
		elseif (!@mysql_query("SET CHARACTER SET '".$this->charset."'", $this->dbConn))
		{
			echo "Napaka pri izbiri charseta!";
			exit;
		}elseif (!@mysql_query("SET NAMES '".$this->charset."'", $this->dbConn))
		{
			echo "Napaka pri izbiri charseta!";
			exit;
		}
    }

	//query v databazo
	function query($sql,$errors=false)
	{
		if (!$query = mysql_query($sql, $this->dbConn))
			{
				//preverimo spremenljivko
				$clean['sporocilo'] = inputSQLcheck(mysql_error($this->dbConn)."<br />Napaka na SQLju: ".$sql."<br />".debug_print_backtrace());

				if($this->noerror) {
					//error
					$this->error->log("Napaka na SQL poizvedbi v databazo!", "echo", $clean['sporocilo']);
				}
				else
				{
					echo "Napaka na SQL poizvedbi v databazo! <br/>".$clean['sporocilo'];
				}
			}
		return new MysqlResult($this, $query);
	}
}

/* MysqlResult class, @access Public  */
/* class pobere podatke iz databaze   */
/**************************************/
class MysqlResult
{
	var $query;
	var $mysql;

	//constructor
	function mysqlResult(&$mysql, $query)
	{
		$this->mysql = &$mysql;
		$this->query = $query;
	}

	//fetch, vrne podatke dobljene preko querya
	function fetch($asoc = MYSQL_ASSOC)
	{
		if ($row = mysql_fetch_array($this->query, $asoc))
		{
			return $row;
		}
		else
		{
			return false;
		}
	}

	//fetch, vrne samo MYSQL_ASSOC podtke preko queriy
	function fetchTextOnly()
	{
		if ($row = mysql_fetch_array($this->query, MYSQL_ASSOC))
		{
			return $row;
		}
		else
		{
			return false;
		}
	}

	//size, vrne število rezultatov dobljenih preko querya
	function size()
	{
		return mysql_num_rows($this->query);
	}

	//preveri če vnos obstaja v bazi
	function exist()
	{
		if($this->size() == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	//vrnje zadnji vnešen ID v bazo
	function insertID()
	{
        return mysql_insert_id($this->mysql->dbConn);
   }
   
}
?>