<?php
// Database Info
$host = "localhost";
$dbName = "digitalb_beaccurate";
$userName = "digitalb_beaccurate";
$pass = "Saiful.1633";

try {
    // Establish database connection using PDO
    $pdo = new PDO("mysql:host=$host;dbname=$dbName;charset=utf8", $userName, $pass);
    // Set PDO to throw exceptions on errors
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // SQL query to fetch all data from the Products table
    $stmt = $pdo->query("SELECT * FROM Products");
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Encode the PHP array into JSON format
    header('Content-Type: application/json');
    echo json_encode($results, JSON_PRETTY_PRINT);

} catch (PDOException $e) {
    // Handle database connection errors
    http_response_code(500); // Set HTTP status code to 500 (Internal Server Error)
    echo json_encode(["error" => "Database connection failed: " . $e->getMessage()]);
} finally {
    // Close the database connection
    $pdo = null;
}
?>