<?php
$host = 'localhost';
$dbname = 'Name_Password';
$user = 'pail';
$password = 'password';

try {
    $dsn = "mysql:host=$host;dbname=$dbname";
    $pdo = new PDO($dsn, $user, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // enable error mode
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
    exit(); // terminate the script on connection failure
}

if (isset($_GET['refresh'])) {
    header("Refresh:0");
}

// Select the Users table
$sql = "SELECT * FROM prihlasovacie_udaje";
$stmt = $pdo->query($sql);

// Output the results
if ($stmt->rowCount() > 0) {
    echo "<table>";
    echo "<tr><th>ID</th><th>Meno</th><th>Heslo</th><th>IP Address</th></tr>";
    
    $counter = 1; // Counter variable
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "<tr><td>" . $counter++ . "</td><td>" . $row["FirstName"] . "</td><td>" . $row["Password"] . "</td><td>" . $row["IPAddress"] . "</td></tr>";
    }
    
    echo "</table>";
} else {
    echo "0 results";
}

// close the database connection
$pdo = null;
?>
<!DOCTYPE html>
<html>
<head>
    <title>View Users</title>
	   <style>
        table {
            border-collapse: collapse;
            width: 50%;
        }
        
        th, td {
            padding: 8px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        
        th {
            background-color: #9ACD32;
        }
    </style>
</head>
<body>
<br>
<button onclick="location.href='?refresh=true'">Obnoviť databázu</button>
</body>
</html>