
<?PHP

/*
This class handles all queries to the database for Planeshifts server console. It has two methods:
query_db and get_current_result along with one property db_err which holds the error messages for any errors which occur
if any of the methods return 0 as their value check the db_err property for the error message since a problem has occured
and the operation failed. Additionally the db_err property can be checked on creation of the object for errors if it's value is
0 then no errors occured and a connection to the database was successful.
*/

class db_connection 
{

    // The following values can be overridden by the constructor these are the default values
    var $db_hostname = "localhost"; // hostname of the db connection
    var $db_name = "planeshift"; // the name of the database you are connecting to on the server
    var $db_username = "planeshift"; // username you are connecting under
    var $db_password = "planeshift"; // the password for connecting

    // The following vairables are dynamic and require the constructor and methods to set their values
    var $db_conn; // holds the connection object to the database
    var $db_result; // contains the result of the last query run against the database.
    var $db_err = 0; // contains error message from last operation if any
    var $arIndex = 0; // index value for the array holding the rows
    var $db_resultset; // array containing all the rows from the query       


    // setup the db with the given values
    function db_setup ( )
    {


    }


    // this is the constructor function for the class it sets a connection to the database if an error occurs
    // then it sets $db_error to the error message anytime this object gets created that internal variable
    // should be checked for an error message a value of 0 means no errors occured                       
    function db_connection ( $db_hostname = "", $db_name = "", $db_username = "", $db_password = "") 
    {
                        
        if ( $db_hostname ) {
          $this->db_hostname = $db_hostname;
          $this->db_name = $db_name;
          $this->db_username = $db_username;
          $this->db_password = $db_password;
        }

        $this->db_conn = mysql_connect($this->db_hostname, $this->db_username, $this->db_password)
          or die("connection failed: " . mysql_error());
        mysql_select_db($this->db_name, $this->db_conn)
            or die("database selection failed: " . mysql_error());
    }
    function useDB( $databaseName = "" )
    {
        if ( $databaseName )
            $this->db_name = $databaseName;
        mysql_select_db($this->db_name, $this->db_conn)
            or die("database selection failed: " . mysql_error());
    }

    //this method runs a query against the current database and  returns
    //the results of the query it then resets the result-set                        
    function query_db ($query)
    {
        //echo "executing: $query<br>";
        $this->db_err = 0; // reset error code for this operation
                      
        $this->$db_result = mysql_query( $query )
            or die("INVALID: " . mysql_error() . $query );

        if ($this->db_err == 0)   
        {
            settype ($this->db_resultset,  "array");
                 
            //clear the values stored from last query
            foreach ($this->db_resultset as $i => $value) 
            {
                unset($this->db_resultset[$i]);
            }
            $this->arIndex = 0;
                 
            if (is_resource($this->$db_result)) 
            {                  
                while ($line = mysql_fetch_array($this->$db_result, MYSQL_BOTH))
                {
                    foreach ($line as  $key => $value) 
                    {
                        $this->db_resultset[$this->arIndex][$key] = $value;
                    }
                    $this->arIndex ++;
                }
                return $this->db_resultset;  
                mysql_free_result($this->$db_result);              
            } 
            else 
            {
                return 0;
            }                                  
        }
    }
      
    // function to retrieve the results of the last query against the database in case someone needs it    
    function get_current_result () 
    {
        if ($this->db_err == 0) 
        {
            return $this->$db_resultset;
        } 
        else
        {
            return 0;
        }
    }    
    
    function next_id( $field, $table )
    {
        $query_string = "select max($field) from $table";
        $result = mysql_query( $query_string );
        
        if ( is_resource($result) )
        {
            $line = mysql_fetch_array($result, MYSQL_NUM);
            $newid = $line[0] + 1;
            return $newid;
        }
        else
        {
            return 1;
        }
    }
    
    function get_num_rows()
    {
        if ( is_resource($this->$db_result) )
            return mysql_num_rows( $this->$db_resultset );
        else
            return 0;
    }   
}
