<?php
// Include database connection
require("../../includes/config.php");


// Set header for JSON response
header("Content-Type: application/json");

// Prepare the response
$response = array('status' => '', 'message' => '', 'data' => array());

try {
    // SQL query to fetch all students
    $sql = "SELECT id, student_name FROM students";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    // Fetch all students
    $students = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if ($students) {
        $response['status'] = 'success';
        $response['data'] = $students;
    } else {
        $response['status'] = 'error';
        $response['message'] = 'No students found.';
    }
} catch (PDOException $e) {
    // Error handling: catch any PDO errors
    $response['status'] = 'error';
    $response['message'] = 'Database Error: ' . $e->getMessage();
}

// Return the response as JSON
echo json_encode($response);
?>
