#native_company# #native_desc#
#native_cta#

Mysql Class.

By Mark Langridge
on February 15, 2002

Version: 1

Type: Class

Category: Databases

License: GNU General Public License

Description: Easy to use mysql class that has basic database access with error checking.

<?php

/*

This class holds all the functions needed for basic mysql database access.

functions within this class are.

    the constructor. mysql_dbase which is called with database name, hostname, username, and password. or they can be set to be defaults, The dbname default has to be left as test for internal reasons. this means when creating a new instance of this class the minimum call is normally: $inst=new mysql_dbase("dbname"); unless you are creating a database. in which you can leave the name blank.

All functions will halt processing and display an error message in the browser on a fault in any of the commands.

    connect()			: no options just creates the connection 
 				  to the database.
    use_db(name)		: Change the current database to the one named.
    make_db(name)		: Creates a new database with the given name.
    drop_db(name)		: Drops the database with the given name.
				  Note: this function does not seek confirmation
				  and is none recoverable.
    do_query(query)		: Performs the given query and will return the
				  number of rows affected or returned for each
				  query.
    get_data(type)		: Returns an array of data base on the type
     				  supplied. A is associative, N is numeric, or
				  B for both.
    close_db() 			: Closes the database connection.

*/

class mysql_dbase
{
    var $dbname;		// name of database to connect to
    var $hostname;		// Host name of the database
    var $uname;			// Username to connect with
    var $pword;			// password to use
    var $conn;			// Used internally to track a connection
    var $result;   		// Used internally to track a query

    function mysql_dbase($dbname="test", $hostname="localhost", $username="nobody", $password="password") 
    {
	$this->dbname=$dbname;
        $this->hostname=$hostname;
        $this->uname=$username;
        $this->pword=$password;
    } 

    function connect()
    {
        $this->conn=mysql_connect($this->hostname, $this->uname, $this->pword);
        if ( !$this->conn )
        {
	    echo "<BR>nConnection to database ".$dbname." has failed.<P>n";
            mysql_error();
            exit;
        }

        if ( $this->dbname != "test" )
        {
            $result=mysql_select_db($this->dbname, $this->conn);
            if ( !$result )
            {
                echo "Failed to select database ".$this->dbname."<P>n";
                echo mysql_error();
                exit;
            }
        }

    }

    function use_db($dbname) 
    {
	if ( $dbname == "" )
	{
	    echo "ERROR : No database name supplied to use_db";
	    exit;
	}
        $result=mysql_select_db($dbname, $this->conn);
        if ( !$result )
        {
	    echo "Failed to select database ".$dbname."<P>n";
	    echo mysql_error();
	    exit;
        }
        $this->dbname=$dbname;
    }

    function make_db($dbname)
    {
	if ( $dbname == "" )
	{
	    echo "ERROR : No database name supplied to make_db";
	    exit;
	}
	$result=mysql_create_db($dbname, $this->conn);
	if ( !$result )
	{
	    echo "Failed to create database ".$this->dbname."<P>n";
	    echo mysql_error();
	    exit;
	    
	}
	$this->dbname=$dbname;
	$result=mysql_select_db($this->dbname, $this->conn);
	if ( !$result )
	{
	    echo "Failed to select database ".$this->dbname."<P>n";
	    echo mysql_error();
	    exit;
	}
	return(1);
    }

    function drop_db($dbname)
    {
	if ( $dbname == "" )
	{
	    echo "ERROR : No database name supplied to drop_db";
	    exit;
	}
	$result=mysql_drop_db($dbname, $this->conn);
	if ( !$result )
	{
	    echo "Failed to drop database ".$dbname."<P>n";
	    echo mysql_error();
	    exit;
	    
	}
	return(1);
    }

    function do_query($query, $num="n")
    {
	 if ( $query == "" )
	 {
	     echo "ERROR : No query supplied to do_query";
	     exit;
	 }
         $this->result=mysql_query($query, $this->conn);
         if ( !$this->result )
         {
             echo "Query ".$query." on database ".$this->dbname." has failed<P>n";
	     echo mysql_error();
             exit;
         }      
         $retval=0;  
	 if ( (stristr($query, "delete")) || (stristr($query, "insert")) || (stristr($query, "update")) )
         {
             $retval=mysql_affected_rows($this->conn);
         }
         else
         {
	     $retval=mysql_num_rows($this->result);
         } 
	 return($retval);
    }

    function get_data($type="B")
    {
        if ( $type == "A" )
        {
	    $fields=mysql_fetch_array($this->result, MYSQL_ASSOC);
        }
        elseif ( $type == "N" )
        {
 	    $fields=mysql_fetch_array($this->result, MYSQL_NUM);
        }
        else
        {
	    $fields=mysql_fetch_array($this->result);
        }
        return($fields);
    }

    function close_db()
    {
        $result=mysql_close($this->conn);
        if ( !$result )
        {
            echo "Failed to close database - ".$this->dbname."<P>n";
	    echo mysql_error();
        }
    }
}

?>