<?php
header('Content-Type: application/json');

try {
    $db = new PDO("sqlite:database.db");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $db->query("SELECT user_id, name, picture, sex FROM students");

    $students = [];

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        // convert BLOB image to base64 for transfer
        $row['picture'] = base64_encode($row['picture']);

        $students[] = $row;
    }

    echo json_encode($students);
} catch (Exception $e) {
    echo json_encode(["error" => $e->getMessage()]);
}
