MySQL-DB-Klasse (auch für PHP4)

Ein paar Codeschnipsel: diesmal eine kleine PHP-Klasse, die den Zugriff auf MySQL-Datenbanken wesentlich vereinfacht:

class CDB
{
    var $sql;
    var $erg;

    var $server;
    var $benutzer;
    var $passwort;
    var $db;
    var $verbindung;

    function CDB($server=null, $benutzer=null, $passwort=null, $db=null)
    {
        $this->Init($server, $benutzer, $passwort, $db);
    }

    function Init($server=null, $benutzer=null, $passwort=null, $db=null)
    {
        if($server != null)
            $this->server = $server;
        else
            $this->server = "localhost";

        if($benutzer != null)
            $this->benutzer = $benutzer;
        else
            $this->benutzer = "DBUSER";

        if($passwort != null)
            $this->passwort = $passwort;
        else
            $this->passwort = "DBPASSWORD";

        if($db != null)
            $this->db = $db;
        else
            $this->db = "DBNAME";

        $this->erg = null;
        $this->sql = null;

        $this->verbindung = @mysql_connect($this->server, $this->benutzer, $this->passwort);
        mysql_select_db ($this->db, $this->verbindung);
    }

    function SetSQL($sql)
    {
        $this->sql = $sql;
    }

    function GetSQL()
    {
        return($this->sql);
    }

    function Query()
    {
        $this->FreeResult();

        if($this->IsConnectionValid() && $this->IsQuerySet())
            $this->erg = mysql_query($this->sql, $this->verbindung);
        else
            $this->erg = null;
    }

    function SetSQLandQuery($sql)
    {
        $this->SetSQL($sql);
        $this->Query();

        return($this->GetResult());
    }

    function GetResult()
    {
        if($this->IsResultValid())
            return($this->erg);
        else
            return(null);
    }

    function GetResultObject()
    {
        if($this->IsResultValid())
            return(mysql_fetch_object($this->erg));
        else
            return(false);
    }

    function GetResultArray()
    {
        if($this->IsResultValid())
            return(mysql_fetch_array($this->erg, MYSQL_BOTH));
        else
            return(false);
    }

    function FreeResult()
    {
        if($this->IsResultValid())
            @mysql_free_result($this->erg);
    }

    function GetAffectedRows()
    {
        return(mysql_affected_rows());
    }

    function GetNumRows()
    {
        if($this->IsResultValid())
            return(mysql_num_rows($this->erg));
        else
            return(null);
    }

    function GetInsertID()
    {
        if($this->IsConnectionValid())
            return(mysql_insert_id($this->verbindung));
        else
            return(null);
    }

    function IsResultValid()
    {
        if($this->erg && $this->erg != null)
            return(true);
        else
            return(false);
    }

    function IsConnectionValid()
    {
        if($this->verbindung &&  $this->verbindung != null)
            return(true);
        else
            return(false);
    }

    function IsQuerySet()
    {
        if($this->sql != null)
            return(true);
        else
            return(false);
    }

    function GetLastError()
    {
    	if($this->IsConnectionValid())
			return(mysql_error($this->verbindung));
		else
			return("");
	}
}

Eingebunden ist das auch sehr schnell. Beispiel:

$db = new CDB();
$sql = "select * from table";
if($db->SetSQLandQuery($sql))
{
	while($row = $db->GetResultObject())
	{
		...
	}
}

Wenn die Einträge in der Init-Methode auf eine aktuelle DB-Konfiguration verweisen, muss im sonstigen Quellcode kein Connect oder eine andere DB-Operation durchgeführt werden. Objekt instanzieren, SQL reinschicken, abfragen. Sonst nichts.

Du kannst alle Antworten zu diesem Eintrag via RSS 2.0 Feed erfolgen. Du kannst einen Kommentar hinterlassen, oder einen Trackback von deiner eigenen Seite.

Einen Kommentar hinterlassen

XHTML: Diese Tags kannst Du benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>