MySQL-Datenbank-Klasse

Dipsi

Mitglied
Hallo

Hab bis jetzt noch nicht viel mit OOP gemacht. (Vor ein paar Wochen habe ich mir ein paar Tipps im Forum geholt.)
Jetzt habe ich mal eine Datenbank Klasse geschrieben und wollte mal von Euch hören, ob
das von der Performance usw. eine gute Lösung ist.

PHP:
<?php
// Exception Class
class Except extends Exception
{
  // Variable
  protected $arrMsg = array();
  
  // Construct
  public function __construct()
  {
    $args = func_get_args();
    $code = 0;
    
    // Put the Arguments in a Array
	if(count($args) != 0)
    {
	  // Parent Construct
	  parent::__construct($args[0], $code);
	  
	  foreach($args as $n => $arg)
	  {
	    $this->arrMsg[$n] = $arg; 
	  }  
	}
	else
	{
	  $this->arrMsg[0] = 'Nicht definierter Fehler';
	}  
  }
  
  // Destruct
  public function __destruct()
  {
    unset($this->arrMsg);
  }
  
  // String Function  
  public function __toString()
  {
    $strOutput;
    
    // Prepare the Output
	foreach($this->arrMsg as $n => $arg)
    {
	  $strOutput.= ' '.$this->arrMsg[$n];
	}
    return __CLASS__ . '-> '.$strOutput;
  }
}
?>

//-------------------------------------------------------------------------------------------------------------

<?php
// Includes
require_once('class.exception.php');
require_once('class.mysql_query.php');

// Connect Exception
class conException extends Except{};

// Database select Exception
class dbException extends Except{};

// Database Connection Class
class MySQL_DB
{
  // Variable
  protected $strUser;
  protected $strPw;
  protected $strHost;
  protected $strDB;
  protected $hDB;
  protected $bCon;
  protected $bDB;

  // Constructor
  public function __construct($strDBHost, $strDBUser, $strDBPw, $strDB)
  {
    $this->strUser = $strDBUser;
    $this->strPw = $strDBPw;
    $this->strHost = $strDBHost;
    $this->strDB = $strDB;
    $this->bCon = false;
    $this->bDB = false;
  }
  
  // Destructor
  public function __destruct()
  {
    @mysql_close($this->hDB);
    unset($this->hDB);
  }
	
  // Connection
  public function connect()
  {
    // Try to Connect
    try
    {
      $this->hDB = @mysql_pconnect($this->strHost, $this->strUser, $this->strPw);
      if(!is_resource($this->hDB))
      {
        throw new conException('Datenbank Fehler: ', mysql_errno(), mysql_error());
        $this->bCon = false;
      }
      else if(!mysql_select_db($this->strDB,$this->hDB))
      {
        throw new dbException('Datenbank Fehler: ', mysql_errno(), mysql_error());
        $this->bDB = false;
	    }
    }
    // Catch the Exception's
    catch(conException $e)
    {
      echo $e;
    }
    catch(dbException $e)
    {
      echo $e;
    }
  }
  
  // Query handling
  public function query($strQuery)
  {
    if(!$this->hDB)
	  {
	    $this->connect();
	  }
	  return new MySQL_Query($this->hDB, $strQuery);  
  }
}
?>

//--------------------------------------------------------------------------------------------

<?php
// Query Exception
class queryException extends Except{};

// Database Query Class
class MySQL_Query
{
  // Variable
  protected $hDB;
  public $strQuery;
  protected $dbRes;
  protected $strDBbind = array();
	protected $arrRes = array();
  
  // Construct
  public function __construct($handle, $strQuery)
  {
    $this->hDB = $handle;
    $this->strQuery = $strQuery; 
  }
  
  // Destruct
  public function __destruct()
  {
    unset($this->hDB);
    unset($this->strQuery);
    unset($this->strDBbind);
  }

  // Processes the queries
  public function execute()
  {
    $args = func_get_args();
    foreach($args as $n => $strBind)
    {
      $this->strDBbind[$n + 1] = $strBind;
	  }
	  $cpyQuery = $this->strQuery;

    // Create the query
    foreach($this->strDBbind as $n => $strBnd)
	  {
      $cpyQuery = str_replace(":".$n."", "".mysql_escape_string($strBnd)."", $cpyQuery);
	  }
    
    // Try to send query to the Database
    try
    {
      $res = mysql_query($cpyQuery);
      if(!$res)
      {
        throw new queryException('Abfrage Fehler: ', 'Query failed');
      }
      else
      {
        $this->dbRes = $res;
	    }
    }
    catch(queryException $e)
    {
      echo $e;
		}
  }
  
  // Get Data
  public function fetch_assoc()
  {
    try
    {
      $arrRes = @mysql_fetch_assoc($this->dbRes);
      if(!$arrRes)
      {
        throw new queryException('Abfrage Fehler:', 'Query failed');
      }
      else
      {
        $this->arrRes = $arrRes;
      }
    }
    catch(queryException $e)
    {
      echo $e;
    }
  }
	
	// Fetch
	public function fetch($dbField)
	{
		return $this->arrRes[$dbField];
	}
  
  // Get count rows
  public function num_rows()
  {
    try
    {
      $nRows = @mysql_num_rows($this->dbRes);
      if(!$nRows)
      {
        throw new queryException('Abfrage Fehler:', 'Query failed');
      }
      else
      {
        return $nRows;
      }
    }
    catch(queryException $e)
    {
      echo $e;
    }
  }   
  
  // Get the insert id
  public function insert_id()
  {
    return mysql_insert_id();
  } 
}
?>

Sorry das ich gerade so viel Code hier rein kopiere.
Wäre froh, wenn ich mal ne Rückmeldung bekommen würde.;)
 
Zurück