diff --git a/index.php b/index.php index 51c0daf002b4611ba56b0e4ac0b032b6824b2288..570089eeb9699cc2fb318ae195721e3afd831c62 100644 --- a/index.php +++ b/index.php @@ -1,38 +1,61 @@ <?php -// Database configuration $servername = "db"; $username = "Lab2"; $password = "Ruk241246<3"; $dbname = "Lab2"; -try { - // Create connection - $conn = new mysqli($servername, $username, $password, $dbname); +// Create connection +$conn = new mysqli($servername, $username, $password, $dbname); - // Check connection - if ($conn->connect_error) { - throw new Exception("Connection failed: " . $conn->connect_error); - } +// Check connection +if ($conn->connect_error) { + die("Connection failed: " . $conn->connect_error); +} - // Query to fetch students - $sql = "SELECT id, fname, lname FROM student"; - $result = $conn->query($sql); +// 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(); -} +<!DOCTYPE html> +<html lang="th"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Student List</title> + <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> +</head> +<body class="bg-light"> + <div class="container mt-5"> + <h2 class="text-center text-primary">รายชื่อนักศึกษา</h2> + <div class="card shadow-sm p-4"> + <?php if ($result->num_rows > 0): ?> + <table class="table table-striped"> + <thead class="table-dark"> + <tr> + <th>ID</th> + <th>ชื่อ</th> + <th>นามสกุล</th> + </tr> + </thead> + <tbody> + <?php while ($row = $result->fetch_assoc()): ?> + <tr> + <td><?= htmlspecialchars($row['id']) ?></td> + <td><?= htmlspecialchars($row['fname']) ?></td> + <td><?= htmlspecialchars($row['lname']) ?></td> + </tr> + <?php endwhile; ?> + </tbody> + </table> + <?php else: ?> + <p class="text-danger text-center">ไม่พบข้อมูลนักศึกษา</p> + <?php endif; ?> + </div> + </div> +</body> +</html> + +<?php +$conn->close(); ?>