<?php

$host = 'localhost';
$db   = 'penuel_e';  // Change to your actual database name
$user = 'penuel_e';         // Change to your database username
$pass = 'kolade4jj@gmail.com';             // Change to your database password
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";

$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, // throw exceptions on errors
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,       // fetch as associative array
    PDO::ATTR_EMULATE_PREPARES   => false,                  // use native prepared statements
];

try {
    $pdo2 = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
    echo "Database connection failed: " . $e->getMessage();
    exit;
}


$q2 = intval($_GET['q']); 

$cono = mysqli_connect('localhost','penuel_penuel','vEqcN.6$yF(T');
if (!$cono) {
  die('Could not connect: ' . mysqli_error($cono));
}
$conon = mysqli_connect('localhost','penuel_exam','admin@mail.com');
if (!$conon) {
  die('Could not connect: ' . mysqli_error($conon));
}

mysqli_select_db($cono,"penuel_penuel");
$sql="SELECT * FROM students WHERE id = '".$q2."'";
$result = mysqli_query($cono,$sql);

if($row = mysqli_fetch_array($result)) {
 $student_id=$row['Admission_num'];
 echo $student_id; }





$stmt = $pdo2->prepare("
  SELECT ss.*, ts.subject_name, et.term
  FROM student_scores ss
  JOIN term_subjects ts ON ss.subject_id = ts.id
  JOIN exam_terms et ON ss.term_id = et.id
  WHERE ss.username = ?
  ORDER BY ss.taken_at DESC
");
$stmt->execute([$student_id]);
$results = $stmt->fetchAll();
?>
<?php require 'beg.php'; ?>
<h2>ðŸ“ˆ Your Results</h2>
<table border="1" cellpadding="5">
  <tr>
    <th>Subject</th>
    <th>Term</th>
    <th>Score</th>
    <th>Total</th>
    <th>Date</th>
  </tr>
  <?php foreach ($results as $res): ?>
    <tr>
      <td><?= $res['subject_name'] ?></td>
      <td><?= $res['term'] ?></td>
      <td><?= $res['score'] ?></td>
      <td><?= $res['total_questions'] ?></td>
      <td><?= $res['taken_at'] ?></td>
    </tr>
  <?php endforeach; ?>
</table>
<?php require 'end.php'; ?>