<?php
class DB {
        var $Host = '';
        var $Database = '';
        var $User = '';
        var $Password = '';
        var $Link_ID = 0;
        var $Query_ID = 0;
        var $Record        = array();
        var $Row;
        var $Errno = 0;
        var $Error = "";

  function DB($dbHost = '', $dbDatabase = '', $dbUser = '', $dbPassword = '')
        {
                $this->Host = $dbHost;
                $this->Database = $dbDatabase;
                $this->User = $dbUser;
                $this->Password = $dbPassword;
        }

        function halt($msg)
        {
                echo("</TD></TR></TABLE><B>Datenbank Fehler / Database error:</B> $msg<BR>\n");
                echo("<B>MySQL error</B>: $this->Errno ($this->Error)<BR>\n");
                die("Session halted.");
        }

        function connect()
        {
                if($this->Link_ID == 0)
                {
                        $this->Link_ID = mysql_pconnect($this->Host, $this->User, $this->Password);
                        if (!$this->Link_ID)
                        {
                                $this->halt("Link_ID == false, connect failed");
                        }
                        $SelectResult = mysql_select_db($this->Database, $this->Link_ID);
                        if(!$SelectResult)
                        {
                                $this->Errno = mysql_errno($this->Link_ID);
                                $this->Error = mysql_error($this->Link_ID);
                                $this->halt("cannot select database <I>".$this->Database."</I>");
                        }
                }
        }

        function query($Query_String)
        {
                $this->connect();
                $this->Query_ID = mysql_query($Query_String,$this->Link_ID);
                $this->Row = 0;
                $this->Errno = mysql_errno();
                $this->Error = mysql_error();
                if (!$this->Query_ID)
                {
                        $this->halt("Invalid SQL: ".$Query_String);
                }
                return $this->Query_ID;
        }

        function next_record()
        {
                $this->Record = mysql_fetch_array($this->Query_ID);
                $this->Row += 1;
                $this->Errno = mysql_errno();
                $this->Error = mysql_error();
                $stat = is_array($this->Record);
                if (!$stat)
                {
                        mysql_free_result($this->Query_ID);
                        $this->Query_ID = 0;
                }
                return $this->Record;
        }

        function num_rows()
        {
                return mysql_num_rows($this->Query_ID);
        }

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

        function optimize($tbl_name)
        {
                $this->connect();
                $this->Query_ID = @mysql_query("OPTIMIZE TABLE $tbl_name",$this->Link_ID);
        }

        function clean_results()
        {
                if($this->Query_ID != 0) mysql_free_result($this->Query_ID);
    $this->Query_ID = 0;
        }

        function close()
        {
                if($this->Link_ID != 0) mysql_close($this->Link_ID);
        }
}
?>