Das Beispiel wurde jetzt objektorientiert programmiert. Es sind einige Veränderungen zu erkennen. Detailierte Informationen dazu gibt es im Kapitel OOP.

 Download

 

<!DOCTYPE html>
<html lang="de" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset='utf-8'/>
<title>Minimal Datenbankbeispiel</title>
<style>
    #rahmen
    {
        margin:0 auto;
        width:60em;      
    }
 
    #content
    {
        width:48em;
        float:right;
        padding: 1em;
    }
    .bigtab
    {
        border:1px solid black; border-collapse:collapse; background:#ccc; width:20em; margin-bottom:1em;
    }
    .smalltab
    {
        border:1px solid black; border-collapse:collapse; background:#ccc; width:10em;
    }
</style>
</head><body>
<div id="rahmen">
  
    <div id="content">      
<?php
 
//Objektinstanziierung
$meinTestObjekt=new Testklasse;
 
//Ablaufsteuerung
if(isset($_REQUEST['subject']))$sub=$_REQUEST['subject']; else $sub="";
switch($sub)
{
   case "":$meinTestObjekt->menue(); break;
   case "install":$meinTestObjekt->install(); break;
   case "save":$meinTestObjekt->speichern(); break;
   case "show":$meinTestObjekt->anzeigen(); break;
}
 

//Klassendeklaration
class Testklasse
{
   // Klassenvariablen
   private $mysqldatenbank="minimalDB";
   private $mysqlusername="root";
   private $mysqlpasswort="";
  
   // Klassenfunktionen
   public function menue()
   {  
      $s="<h2>Minimal Datenbankbeispiel</h2>";
      $s.="<button onclick='window.location.href=\"minidb.php?subject=install\"'>Installation</button><br><br>";
      $s.="<button onclick='window.location.href=\"minidb.php?subject=save\"'>Daten Speichen</button><br><br>";
      $s.="<button onclick='window.location.href=\"minidb.php?subject=show\"'>Daten anzeigen</button><br><br>";
      echo $s;
   }
 
 
   public function speichern()
   {
       $username="Testuser";
       $email="Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein! ";
       $eintrag="Dies ist nur ein Testeintrag!";
   
       $conn=$this->inidb();
     
       $da=date("Y-m-d H:i:s");
       $sql="INSERT INTO gastbuch SET name='$username',email='$email',eintrag='$eintrag',datum='$da'";
       if (!mysqli_query($conn, $sql))echo mysqli_error($conn);
       mysqli_close($conn);
    
      $this->menue();
   }
 
   public function anzeigen()
   {
       $conn=$this->inidb();
    
       $result = mysqli_query($conn, "SELECT * FROM gastbuch ORDER BY nummer DESC");
       if (mysqli_num_rows($result) > 0)
       {
           while($row = mysqli_fetch_assoc($result))
           {
               $s="<table class='bigtab'>";
               $s.="<tr><td>Name: ".$row['name']."</td></tr>";
               $s.="<tr><td>Email: ".$row['email']."</td></tr>";
               $s.="<tr><td>schrieb am ".$row['datum']."</td></tr>";
               $s.="<tr><td>".$row['eintrag']."</td></tr>"; 
               $s.="</table>\n";
               echo $s;
           }
       }
       mysqli_close($conn);
      $this->menue();
   }
   
   // ************ MySQL-Funktionen *************
   private function inidb()
   {  
       $conn = mysqli_connect("localhost", $this->mysqlusername, $this->mysqlpasswort,$this->mysqldatenbank);
       if (!$conn) {
           die("Connection failed: " . mysqli_connect_error());
       }
       return $conn;
   }   
      
   public function install()
   {
       //Verbindung zum MySQL-Server herstellen   
       $conn = mysqli_connect("localhost", $this->mysqlusername, $this->mysqlpasswort);
       if (!$conn) {
           die("Connection failed: " . mysqli_connect_error());
       }
   
       //Datenbank erstellen
       if (mysqli_query($conn, "CREATE DATABASE ".$this->mysqldatenbank))
       {
           echo "Database created successfully<br>";
       } else {
           echo "<br>Error creating database: " . mysqli_error($conn);
       }
       mysqli_close($conn);
   
       $conn = mysqli_connect("localhost", $this->mysqlusername, $this->mysqlpasswort,$this->mysqldatenbank);
       if (!$conn) {
           die("Connection failed: " . mysqli_connect_error());
       }
   
      
       //Tabelle erstellen
       $sql="CREATE TABLE gastbuch (nummer INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,";
       $sql.="name TEXT, datum TEXT, eintrag TEXT, email TEXT) ENGINE = InnoDB CHARACTER SET=utf8 COLLATE utf8_bin";
       if (mysqli_query($conn, $sql)) {
       echo "Table MyGuests created successfully";
       } else {
           echo "<br>Error creating table: " . mysqli_error($conn);
       }
   
   
       echo "<p>MySQL-Installation beendet!";
       mysqli_close($conn);
      $this->menue();
   }
  
} //Ende der Klasse


?>
    </div> <!-- content -->
</div> <!-- rahmen -->
</body>
</html>