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
// 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);
die("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
<!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();
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment