Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit d2570293 authored by 65160264's avatar 65160264
Browse files

Edit connect index.php

parent 7856506a
No related branches found
No related tags found
No related merge requests found
<?php <?php
// Database configuration
$servername = "db"; $servername = "db";
$username = "Lab2"; $username = "Lab2";
$password = "Ruk241246<3"; $password = "Ruk241246<3";
$dbname = "Lab2"; $dbname = "Lab2";
try {
// Create connection // Create connection
$conn = new mysqli($servername, $username, $password, $dbname); $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection // Check connection
if ($conn->connect_error) { if ($conn->connect_error) {
throw new Exception("Connection failed: " . $conn->connect_error); die("Connection failed: " . $conn->connect_error);
} }
// Query to fetch students // Query to fetch students
$sql = "SELECT id, fname, lname FROM student"; $sql = "SELECT id, fname, lname FROM student";
$result = $conn->query($sql); $result = $conn->query($sql);
?>
// Display results <!DOCTYPE html>
if ($result->num_rows > 0) { <html lang="th">
echo "<h2>Student List</h2>"; <head>
echo "<ul>"; <meta charset="UTF-8">
while ($row = $result->fetch_assoc()) { <meta name="viewport" content="width=device-width, initial-scale=1.0">
echo "<li><strong>ID:</strong> {$row['id']} - <strong>Name:</strong> {$row['fname']} {$row['lname']}</li>"; <title>Student List</title>
} <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
echo "</ul>"; </head>
} else { <body class="bg-light">
echo "<p>No students found.</p>"; <div class="container mt-5">
} <h2 class="text-center text-primary">รายชื่อนักศึกษา</h2>
} catch (Exception $e) { <div class="card shadow-sm p-4">
echo "<p style='color:red;'>Error: " . $e->getMessage() . "</p>"; <?php if ($result->num_rows > 0): ?>
} finally { <table class="table table-striped">
// Close connection <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(); $conn->close();
}
?> ?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment