Hallo,
ich habe eine auf PHP5 basierende MySQL-Klasse geschrieben, mit der man Beispielsweise Fehler per E-Mail versenden kann und bei der Verbindung zw. pconnect und connect wählen kann:
	
database.class.php
	
Ich würde mich über Verbesserungsvorschläge freuen ;-)
PS: Ich weiß, dass es schon fertige Klassen für sowas gibt, aber ich erfinde gerne das Rad neu :P
Seggl-hoch-drei
					ich habe eine auf PHP5 basierende MySQL-Klasse geschrieben, mit der man Beispielsweise Fehler per E-Mail versenden kann und bei der Verbindung zw. pconnect und connect wählen kann:
PHP-Code:
	
	
<?php
error_reporting(E_ALL | E_STRICT);
require_once("database.class.php");
$db = new database("localhost", "root", "", true) or die(mysql_error());
$db->set_errormailing(true, "simon@virtual-meetings.de");
$db->set_error_output(true);
$db->set_querylogging(true);
$db->select_db("datenbank");
$db->query("eine abfrage");
echo $db->get_number_of_requests();
echo "<br />";
echo $db->get_last_query();
echo "<br />";
$db->get_queries(true);
?>
PHP-Code:
	
	
<?php
class database
{
      private $connection;
    private $result;
    private $sum_of_requests;
    private $print_errors;
    private $errorlog;
    private $query;
    private $all_queries;
    private $log_queries;
    private $mail_errors;
    private $mailto;
    
    public function __construct($db_host, $db_user, $db_pw, $use_pconnect = true)
    {
       $this->connection       = false;
       $this->result           =    false;
       $this->sum_of_requests = 0;
       $this->print_errors       = false;
       $this->errorlog           = array();
       $this->query           = "";     
       $this->all_queries     = array();
       $this->log_queries     = false;  
       $this->mail_errors     = false;
       $this->mailto          = "";
       
       if($use_pconnect === true)
       {
        $this->connection = mysql_pconnect($db_host, $db_user, $db_pw) or $this->error();
       }
       
        if(!$this->connection)
        {
        $this->connection = mysql_connect($db_host, $db_user, $db_pw) or $this->error();
         if(!$this->connection)
         {
           die("Couldn't establish a connection");
         }
        }
    }
    
    public function __destruct()
    {
      $this->close();
      unset($this->connection);
      unset($this->result);
      unset($this->sum_of_requests);
      unset($this->print_errors);
      unset($this->errorlog);
      unset($this->all_queries);
      unset($this->log_queries);
      unset($this->mail_errors);
      unset($this->mailto);
    }
    
    //--------------------------------------------------------------------------------------------
    /**
       The get-methods
    */
    public function get_connection_id()
    {
      if($this->connection !== false)
      {
        return $this->connection;
      }
      else
      {
        return "There's no connection to any database";
      }
    }
    
    public function get_errorlog($print_it = true)
    {
      if($print_it === true)
      {
        foreach($this->errorlog as $key => $value)
        {
          echo "<br /> <b>".($key+1).":</b> ".$this->errorlog[$key];
        }
      }
      
      //return the errorlog in every case
      return $this->errorlog;      
    }
    
    public function get_queries($print_it = true)
    {
      if($print_it === true)
      {
        foreach($this->all_queries as $key => $value)
        {
          echo "<br /> <b>".($key+1).":</b> ".$this->all_queries[$key];
        }
      }
      
      //return the errorlog in every case
      return $this->all_queries;     
    }
    
    public function get_number_of_requests()
    {
      return (int)$this->sum_of_requests;
    }
    
    public function get_affected_rows($handler)
    {
        if(isset($handler))
        {
             return mysql_num_rows($handler) or $this->error();
        }
        else
        {
            return false;
        }
    }    
    
    public function get_last_query()
    {
      return ((!empty($this->query)) ? $this->query : "no query aviable");
    }
    //--------------------------------------------------------------------------------------------
    
    
    //--------------------------------------------------------------------------------------------
    /**
      The set-methods
    */
    public function set_error_output($boolval = true)
    {
      $value_before = $this->print_errors;
      $this->print_errors = ($boolval === true) ? true : false;
      return $value_before;
    }    
    
    public function set_querylogging($boolval = true)
       {
      $value_before = $this->log_queries;
      $this->log_queries = ($boolval === true) ? true : false;
      return $value_before;
    }
    
    public function set_errormailing($boolval = true, $mailto)
    {
      $value_before = $this->mail_errors;
      $this->mailto =      $mailto;
      $this->mail_errors = ($boolval === true) ? true : false;
      return $value_before;
    }
    
    public function select_db($db_name)
    {
        return mysql_select_db($db_name, $this->connection) or $this->error();
    }
    
    
    //--------------------------------------------------------------------------------------------
    /**
       Other methods of this class
    */
    private function error()
    {
      $this->errorlog[] = mysql_error($this->connection);
      
      if($this->mail_errors === true)
      {
        mail($this->mailto, "MySQL-Error", "The following error occured: ".mysql_error($this->connection));
      }
      
      if($this->print_errors === true)
      {
        die("an error occured: ".mysql_error($this->connection));
      }
    }
    
    public function query($db_query_string)
    {        
        $this->query = $db_query_string;
        
        if($this->log_queries === true)
        {
          $this->all_queries[] = $this->query;
        }
        
        $this->ergebnis = mysql_query($this->query, $this->connection) or $this->error();
        $this->sum_of_requests++;
        return $this->ergebnis;        
    }
    
    public function escape_string($string)
    {
      return mysql_real_escape_string($string, $this->connection);
    }
    
    public function close()
    {
      mysql_close($this->connection);
      $this->connection = false;
    }
    //--------------------------------------------------------------------------------------------
    
}
?>
PS: Ich weiß, dass es schon fertige Klassen für sowas gibt, aber ich erfinde gerne das Rad neu :P
Seggl-hoch-drei
 
          
 Moderator
 Moderator
 
							
						 Gibt doch genug native Unterstützung. Wenn du dich da einarbeitest und die nötigenfalls erweiterst, macht das sicher mehr Sinn!
 Gibt doch genug native Unterstützung. Wenn du dich da einarbeitest und die nötigenfalls erweiterst, macht das sicher mehr Sinn!
							
						
Kommentar