Hi,
- A connection to your MySQL database is established using
mysqli
.
- A prepared statement is used to fetch the course names and statuses for the registered courses of a specific student.
- The results are displayed in an HTML table. If no courses are found, a message indicates that the student is not registered in any courses.
Database Structure
-
courses table:
id
(int, primary key)
course_name
(varchar)
status
(varchar)
-
registrations table:
id
(int, primary key)
student_id
(int)
course_id
(int)
<?php
// db.php - Database connection
$host = 'localhost';
$db = 'your_database';
$user = 'your_username';
$pass = 'your_password';
$mysqli = new mysqli($host, $user, $pass, $db);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Assuming we have the student ID
$student_id = 1; // Replace with dynamic value based on logged-in student
// SQL query to fetch the registered courses and their status
$sql = "SELECT c.course_name, c.status
FROM registrations r
JOIN courses c ON r.course_id = c.id
WHERE r.student_id = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$courses = [];
while ($row = $result->fetch_assoc()) {
$courses[] = $row;
}
$stmt->close();
$mysqli->close();
?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Registered Courses</title></head><body><h1>Your Registered Courses</h1><?php if (empty($courses)): ?><p>You are not registered in any courses.</p><?php else: ?><table border="1"><tr><th>Course Name</th><th>Status</th></tr><?php foreach ($courses as $course): ?><tr><td><?php echo htmlspecialchars($course['course_name']); ?></td><td><?php echo htmlspecialchars($course['status']); ?></td></tr><?php endforeach; ?></table><?php endif; ?></body></html>