60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
$host = getenv('DB_HOST') ?: 'mysql-db';
|
|
$user = getenv('DB_USER') ?: 'root';
|
|
$pass = getenv('DB_PASSWORD') ?: 'secret';
|
|
$dbname = getenv('DB_NAME') ?: 'testdb';
|
|
|
|
echo "<h1>Datenbank-Verbindungstest</h1>";
|
|
echo "<p>Verbindungsversuch zu: $host</p>";
|
|
|
|
try {
|
|
// Verbindung erstellen
|
|
$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
|
|
|
|
// PDO-Fehlerbehandlung aktivieren
|
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
echo "<div style='color:green;'>Verbindung erfolgreich!</div>";
|
|
|
|
// Prüfen, ob Testtabelle existiert, sonst erstellen
|
|
$stmt = $conn->query("SHOW TABLES LIKE 'test_table'");
|
|
if ($stmt->rowCount() == 0) {
|
|
$conn->exec("CREATE TABLE test_table (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
message VARCHAR(255),
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
$conn->exec("INSERT INTO test_table (message) VALUES ('Testdaten 1')");
|
|
$conn->exec("INSERT INTO test_table (message) VALUES ('Testdaten 2')");
|
|
echo "<p>Testtabelle erstellt und mit Daten gefüllt.</p>";
|
|
}
|
|
|
|
// Daten auslesen
|
|
$stmt = $conn->query("SELECT * FROM test_table");
|
|
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
echo "<h2>Daten aus der Datenbank:</h2>";
|
|
echo "<table border='1'>";
|
|
echo "<tr><th>ID</th><th>Nachricht</th><th>Erstellt am</th></tr>";
|
|
|
|
foreach ($rows as $row) {
|
|
echo "<tr>";
|
|
echo "<td>" . $row['id'] . "</td>";
|
|
echo "<td>" . $row['message'] . "</td>";
|
|
echo "<td>" . $row['created_at'] . "</td>";
|
|
echo "</tr>";
|
|
}
|
|
|
|
echo "</table>";
|
|
|
|
} catch(PDOException $e) {
|
|
echo "<div style='color:red;'>Verbindungsfehler: " . $e->getMessage() . "</div>";
|
|
}
|
|
|
|
// Verbindung schließen
|
|
$conn = null;
|
|
?>
|
|
|
|
<p>Seite geladen um: <?php echo date('H:i:s'); ?></p>
|
|
<p><a href="index.php">Zurück zur Startseite</a></p>
|