<?php 
// Database configuration
$servername = "db";
$username   = "Lab2";
$password   = "Ruk241246<3";
$dbname     = "Lab2";

try {
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        throw new Exception("Connection failed: " . $conn->connect_error);
    }

    // Query to fetch students
    $sql = "SELECT id, fname, lname FROM student";
    $result = $conn->query($sql);

    // Display results
    if ($result->num_rows > 0) {
        echo "<h2>Student List</h2>";
        echo "<ul>";
        while ($row = $result->fetch_assoc()) {
            echo "<li><strong>ID:</strong> {$row['id']} - <strong>Name:</strong> {$row['fname']} {$row['lname']}</li>";
        }
        echo "</ul>";
    } else {
        echo "<p>No students found.</p>";
    }
} catch (Exception $e) {
    echo "<p style='color:red;'>Error: " . $e->getMessage() . "</p>";
} finally {
    // Close connection
    $conn->close();
}
?>