db = $db; $this->username = $u; $this->password = $pw; } function getDBName() { return $this->dbname; } // Liefert ein Array. // Jeder Eintrag stelt eine Datenzeile dar, die ein assoziatives Array ist. function get_Query($sql) { try { $dbh = new PDO("mysql:host=" . $this->hostname.";dbname=".$this->db, $this->username, $this->password); $statement = $dbh->prepare($sql); $statement->execute(); $dbh = null; // ** close the database connection *** / } catch(PDOException $e) { echo $e->getMessage(); } return $statement->fetchAll(PDO::FETCH_ASSOC); } // liefert Abfrage im JSON-Format function getJSON_Data($sql) { $result = $this->get_Query($sql); $json_data = json_encode($result); return $json_data; } function getQueryAsHTMLDropDownBox($sql, $link, $default, $dropdownName) { $result = $this->get_Query($sql); $html = ""; if ($result != null) { $html .= "\n"; return $html; } else { return "Error beim erstellen der Dropdown-Box."; } } function getQueryAsTEXTTable($sql,$format) { $result = $this->get_Query($sql); $html = "
\n"; $html .= "
\n";
        if ($result != null) {
            foreach($result as $zeile) {
                $html .= vsprintf($format, $zeile) . "\n";
            }
            $html .= "
\n"; $html .= "
\n"; } else $html = "error"; return $html; } function getQueryAsHTMLTable($sql) { $result = $this->get_Query($sql); $styleHeader = "style='background-color: #4D72D6; color:white;'"; $styleRowLow = "style='background-color:white;'"; $styleRowHigh = "style='background-color:#E1EFF5;'"; if ($result != null) { $html = "\n"; // Tasbellenüberschrift $zeile = $result[0]; $html .= ""; foreach($zeile as $k => $v) { $html .= "" ; } $html .= "\n"; // Tabelleninhalt $high = false; foreach($result as $zeile) { if ($high) { $st = $styleRowHigh; $high=false; } else { $st = $styleRowLow; $high=true; } $html .= ""; foreach($zeile as $k => $v) { $html .= "" ; } $html .= "\n"; } $html .= "
$k
$v
\n"; } else $html = "error"; return $html; } } /* * ------------------------------------------------------ * Testder Methoden * ------------------------------------------------------ $db = new DB("euro","dollar","wechselkurse"); echo "Datenbank " . $db->getDBName() . "\n"; echo "JSON \n"; echo $db->getJSON_Data("SELECT * from kurse LIMIT 2;") . "\n"; echo "PDO \n"; //print_r ( $db->get_Query("SELECT * from kurse LIMIT 2;") ); echo "getQueryAsDropDownBox \n"; //print_r ($db->getQueryAsHTMLDropDownBox("SELECT DISTINCT(waehrung) from kurse ORDER BY waehrung", "https://server.de/show.php?waehrung=", "JPY", "myDDF") ); echo "getQueryAsTEXTTable \n"; //print_r ($db->getQueryAsTEXTTable("SELECT * from kurse LIMIT 4","%-4s %-12s %-5s %10f") ); echo "getQueryAsHTMLTable \n"; print_r ($db->getQueryAsHTMLTable("SELECT * from kurse LIMIT 4") ); */ ?>