<?php
/*
--------------------------------------------------
simple MySql handler class
(c) 2000 Atiware (Attila Soki)
version: 1.0
--------------------------------------------------
*/
	include_once("constants.inc.php");

	class MySql {
		var $errno=null;
		var $errmsg=null;
		var $dbname=null;
		var $res=null;
		var $user=null;
		var $pw=null;
		var $ip=null;
		var $port=null;
		var $db=null;
		var $mr=null;
		var $q=null;
		var $lastErrNo;
		var $lastError;

		var $connecterrno=0;
		var $connecterror=null;
		var $timeout=null;

		var $connected=false;


		//write some debug msg out if $this->dbug==true
		//only for debug query executions
		function checkError($head=null,$query=null) {
			$this->errno=@mysql_errno($this->db);
			$this->errmsg=@mysql_error($this->db);
			if ($this->errno>0) {
				$this->lastErrNo=$this->errno;
				$this->lastError=$this->error;
			} //end if
			if ($this->dbug) {
				printf("Headline:%s<br>\n",$head);
				printf("Handler:%s<br>\n",$this->db);
				printf("Connection:%s<br>\n",($this->connected) ? "ok":"NOT CONNECTED!");
				printf("User:%s<br>\n",$this->user."/".$this->pw);
				printf("Query:%s<br>\n",($this->connected) ? "":" NOT EXECUTED: ".$query);
				printf("ErrorNo:%s<br>\n",$this->errno);
				printf("Error:%s<br>\n",$this->errmsg);
				printf("---END<br>\n");
			}
			return $this->errno;
		}

		//connect to server
		function connect() {
			$this->connected=false;
			$this->db=@mysql_connect($this->ip.":".$this->port,$this->user,$this->pw);
			$this->connected=(int)is_resource($this->db);
			$this->checkError("Connect");
			if (!$this->connected) {
				$this->lastError="MySQL Connection Failed: Access denied for user: '".$this->user."' (Using password: YES)";
			} //end if
			@mysql_select_db($this->dbname,$this->db);
			if (@mysql_errno($this->db)>0)
				$this->connected=false;
			$this->checkError("ChangeDB");
			return $this->db;
		}


		//constructor
		//$w=new MySql() connect to server with server data from constants.inc.php
		//$w=new MySql(true,true) connect to server with server data from constants.inc.php, and debug mode (see checkError)
		//$w=new MySql(false) not connect to server you have to call connect() to make a connection
		//                    in this mode you can set other server data than constants.inc.php (see set**** functions)
		//$w=new MySql(false,true) same as MySql(false) with debug mode
		function MySql($connect=true,$dbug=false) {
			$this->ip=MYSQL_HOST;
			$this->port=MYSQL_PORT;
			$this->user=MYSQL_USER;
			$this->pw=MYSQL_PASSWORD;
			$this->dbname=MYSQL_DB;
			$this->timeout=MYSQL_TIMEOUT;

			$this->connected=false;

//			$dbug=true;
			$this->dbug=$dbug;

			if ($connect)
				$this->connect();
		}

		function setDbName($name) {
			$this->dbname=$name;
		}

		function setPort($port) {
			$this->port=$port;
		}

		function setHost($x) {
			$this->ip=$x;
		}

		function setUser($u) {
			$this->user=$u;
		}

		function setPassword($p) {
			$this->pw=$p;
		}

		function setTimeout($x=null) {
			$this->timeout=$x;
		}

		//closes the connection
		function close() {
			$this->res=null;
			if ($this->db)
				@mysql_close($this->db);
			$this->connected=false;
		}

		//frees the result set
		function freeResult() {
			@mysql_free_result($this->res);
		}

		//returns the connection handler
		function getHandler() {
			return $this->db;
		}

		//gets the last mysql_errno();
		function Errno() {
			return $this->errno;
		}

		//gets the last mysql_error();
		function Error() {
			return $this->errmsg;
		}

		//execute a query and returns a resultset
		//query("select * from ...")
		function query($query) {
			$this->q=$query;
//			if ($this->connected)
				$this->res=@mysql_query($this->q,$this->db);
			if ($this->dbug) {
				printf("NumRows:%s<br>\n",@mysql_num_rows($this->res));
			}
			$this->checkError("ExecuteQuery",$this->q);
			return $this->res;
		}


		//gets the last insertid from mysql if any
		//i use my method to get the last insertid because i don't know what php do
		function insertid() {
			$res1=null;
			$retval=null;
			$res1=@mysql_query("select last_insert_id()",$this->db);
			if (@mysql_num_rows($res1)>0) {
				$mr1=@mysql_fetch_row($res1);
				$retval=$mr1[0];
				@mysql_free_result($res1);
			}
			return $retval;
		}

		//returns the count of rows in current resultset
		function NumRows() {
			return @mysql_num_rows($this->res);
		}

		//returns mysql_fetch_array($thisresultset)
		function fetchArray() {
			return @mysql_fetch_array($this->res);
		}

		/**
		*
		* @access	public
		* @param	 
		* @return	
		*/
		function checkConnection() {
			$retval=false;
			$res1=@mysql_query("select 1 as ONE",$this->db);
			if (@mysql_num_rows($res1)>0) {
				$mr1=@mysql_fetch_row($res1);
				$retval=(int)$mr1[0];
				@mysql_free_result($res1);
			}
			return $retval;
		} //end function

		/**
		*
		* @access	public
		* @param	 
		* @return	
		*/
		function getLastErrorMsg() {
			return $this->lastError;
		} //end function

		/**
		*
		* @access	public
		* @param	 
		* @return	
		*/
		function getLastErrorNo() {
			return $this->lastErrNo;
		} //end function
	}
?>