Meine MySQL-Klasse

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Meine MySQL-Klasse

    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:

    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);

    ?>
    database.class.php
    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;
        }
        
    //--------------------------------------------------------------------------------------------
        
    }
    ?>
    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
    Zuletzt geändert von Seggl-hoch-drei; 26.07.2008, 13:20.
    Tempim.de - Dein kostenloser Bildhoster
    Tipps und Tricks für Webmaster

  • #2
    mit der man Beispielsweise Fehler per E-Mail versenden kann
    Das gehört nicht in die Klasse. Dafür gibt es Exceptions und ggf. den entsprechenden ExceptionHandler, der bei bedarf die E-Mail schickt...

    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);
    }


    Mehr sag ich jetzt erstmal nicht, weil:
    Wenn du z.B. Exceptions verwenden würdest, wäre der Code schon viel übersichtlicher, dann kann man gerne noch mal weiter sehen.

    Ich würd die Klasse direkt auf MySQLi aufsetzen, da haste auch nicht so viel zu erweitern...

    PS: Ich weiß, dass es schon fertige Klassen für sowas gibt, aber ich erfinde gerne das Rad neu :P
    Wenn dass nicht unbedingt das Holzrad wäre, spricht da ja nichts gegen.

    Kommentar


    • #3
      Vielleicht sollte man sagen, dass ich noch nicht besonders viel Erfahrung mit Klassen habe

      Was spricht gegen das Versenden von Emails aus dieser Klasse heraus?
      Tempim.de - Dein kostenloser Bildhoster
      Tipps und Tricks für Webmaster

      Kommentar


      • #4
        Warum denn die tausendste MySQL-Klasse? Gibt doch genug native Unterstützung. Wenn du dich da einarbeitest und die nötigenfalls erweiterst, macht das sicher mehr Sinn!

        Kommentar


        • #5
          Was spricht gegen das Versenden von Emails aus dieser Klasse heraus?
          Das gehört nicht in die Klasse. Dafür gibt es Exceptions und ggf. den entsprechenden ExceptionHandler, der bei bedarf die E-Mail schickt...
          Es gibt in PHP5 endlich eine vernüftige Fehlerbehandlung, einer der gründe, warum sich OOP so langsam auch bei PHP durchgesetzt hat, und verzichtest auf, bzw. ignorierst so elementare Dinge der OOP. Ist halt n bisschen am Ziel vorbei.

          BSP: Wenn du dir jetzt nen File-Upload programmierst, natürlich auch als Klasse. Soll der doch sicher auch wieder bei Fehlern ne E-Mail schicken. Willst du das dann schon wieder neu programmieren?

          Kommentar

          Lädt...
          X