prepare("SELECT * FROM patients WHERE id = ?");
$stmt->execute([$patient_id]);
$patient = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$patient) {
die("Patient not found.");
}
// Fetch all visits for this patient
$visit_history_stmt = db()->prepare(
"SELECT pv.*, u.username as doctor_name
FROM patient_visits pv
LEFT JOIN users u ON pv.doctor_id = u.id
WHERE pv.patient_id = ?
ORDER BY pv.visit_time DESC"
);
$visit_history_stmt->execute([$patient_id]);
$visit_history = $visit_history_stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch all prescriptions for this patient
$prescriptions_stmt = db()->prepare(
"SELECT pr.*, u.username as doctor_name
FROM prescriptions pr
LEFT JOIN users u ON pr.doctor_id = u.id
WHERE pr.patient_id = ?
ORDER BY pr.prescription_date DESC"
);
$prescriptions_stmt->execute([$patient_id]);
$prescriptions = $prescriptions_stmt->fetchAll(PDO::FETCH_ASSOC);
// Fetch all ordered tests for this patient
$ordered_tests_stmt = db()->prepare(
"SELECT
ot.ordered_at,
ot.test_type,
ot.status,
ot.results,
CASE
WHEN ot.test_type = 'lab' THEN lt.test_name
WHEN ot.test_type = 'imaging' THEN it.test_name
END as test_name,
u.username as doctor_name
FROM ordered_tests ot
JOIN patient_visits pv ON ot.visit_id = pv.visit_id
JOIN users u ON pv.doctor_id = u.id
LEFT JOIN lab_tests lt ON ot.test_type = 'lab' AND ot.test_id = lt.test_id
LEFT JOIN imaging_tests it ON ot.test_type = 'imaging' AND ot.test_id = it.test_id
WHERE pv.patient_id = ?
ORDER BY ot.ordered_at DESC"
);
$ordered_tests_stmt->execute([$patient_id]);
$ordered_tests = $ordered_tests_stmt->fetchAll(PDO::FETCH_ASSOC);
?>