Location: PHPKode > scripts > PersistentObject > persistentobject/db.php
<?php
/**
  * @Author:  Paul Campbell <hide@address.com>
  * @Company: Campbell Multimedia <www.campbell-multimedia.com>
  * @Date:    April 2002
  */


class db
{
    var $username;
    var $password;
    var $database;
    var $hostname;
    var $link;
    var $error;
    var $autoerror = true;
    var $autofail = true;

    var $query;
    var $result;
    var $row;
    var $numrows;
    var $numcols;
    var $last_insert;

// Public constructor
    function db( $user, $pass, $dbase, $host="localhost" )
    {
        $this->username = $user;
        $this->password = $pass;
        $this->database = $dbase;
        $this->hostname = $host;
    }

// Sets whether the objects will fail on error and whether they will
// display that error.
    function autofail_on()
    {
        $this->autofail=true;
    }
    function autofail_off()
    {
        $this->autofail=false;
    }
    function autoerror_on()
    {
        $this->autoerror=true;
    }
    function autoerror_off()
    {
        $this->autoerror=false;
    }

    function doerror( $message )
    {
        $this->error = $message;
        if ( $this->autoerror )
        {
            $this->showerror();
        }
        if ( $this->autofail )
        {
            exit;
        }
    }

    function showerror( $span_class = "dberror" )
    {
        $href = "mailto:" . ADMIN_EMAIL;
        $mailto = "<a href='$href'>".ADMIN_EMAIL."</a>";

        $content  = $this->error;
        $content .= "<br />";
        $content .= "Please Email: ";
        $content .= $mailto;
        $content .= "<br />";

        print $content;
    }

// Main function to make sure we have a link.
// RETURN a Mysql db link resource pointer.
    function getlink()
    {
        // If this has NOT got a valid link
        if ( ! @mysql_query( "SHOW STATUS", $this->link ) )
        {
            // Set up the connection
            //if (DEBUG) print "Get Link Called.<br />";
            @$this->link = mysql_connect( $this->hostname,
                $this->username,
                $this->password );

        }
        // Check to see if the pointer is Null.
        if ( ! $this->link )
        {
            $this->doerror( "Failed to connect to Server!");
            return false;
        }

        // Try and select the database.
        if ( ! @mysql_select_db( $this->database, $this->link ) )
        {
            $this->doerror( "DB select Failed! Error:".mysql_error() );
            return false;
        }
        return $this->link;
    }

    function geterror()
    {
        return $this->error;
    }

    function droplink()
    {
        @mysql_close( $this->link );
    }

    function setquery( $query )
    {
        $this->query = $query;
    }

    function runquery( $query = "" )
    {
        if ( ! ($query == "") )
        {
            $this->setquery( $query );
        }
        if ( $this->query == "" )
        {
            $this->doerror( "No Query Present!" );
        }
        $this->getlink();

        if ( ! @$this->result = mysql_query ( $this->query, $this->link ) )
        {
            $this->doerror("Query Error.<br />Error:".mysql_error()."<br />Query:".$this->query );
        }
        if ( strstr( $this->query, "SELECT") or strstr( $this->query, "select"))
        {
            @$this->numrows = mysql_num_rows( $this->result);
            @$this->numcols = mysql_num_fields( $this->result);
        }
        @$this->last_insert =mysql_insert_id( $this->link );
        return $this->link;  // For further testing by client.
    }

    function getnumrows()
    {
        return $this->numrows;
    }

    function getnumcols()
    {
        return $this->numcols;
    }

    function getresult()
    {
        if ( ! $this->result ) $this->runquery();
        return $this->result;
    }

    function getrow()
    {
        if ( ! @$row = mysql_fetch_object( $this->result ) )
        {
            if ( @mysql_error() )
            {
                $this->doerror( "Get Row error: ".mysql_error() );
            }
            return false;
        }
        else
        {
            return $row;
        }
    }
	function getassoc() {
	    if ( ! @$row = mysql_fetch_assoc( $this->result ) )
        {
            if ( @mysql_error() )
            {
                $this->doerror( "Get Row error: ".mysql_error() );
            }
            return false;
        }
        else
        {
            return $row;
        }

	}
	function getarray() {
	    if ( ! @$row = mysql_fetch_array( $this->result ) )
        {
            if ( @mysql_error() )
            {
                $this->doerror( "Get Row error: ".mysql_error() );
            }
            return false;
        }
        else
        {
            return $row;
        }

	}
    function getSingleValue()
    {
        if ( $row = $this->getarray() )
            return $row[0];
    }
    function quick($q, $what)
    {
        $this->runquery($q);
        $row = $this->getrow();
        return $row->$what;
    }
	function affectedRows()
	{
		return mysql_affected_rows( $this->link );
	}
}
?>
Return current item: PersistentObject