<?php
$servername = "localhost";
$username = "venmobdc_Auto_DataBase_User";
$password = "hB_nf_W-^SIh";
$dbname = "venmobdc_Auto_Database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$tableName = $_POST['tableName']; // Get table name from POST request

// Check if table exists
$checkTable = $conn->query("SHOW TABLES LIKE '$tableName'");
if ($checkTable && $checkTable->num_rows == 1) {
    // Fetch data from the specified table
    $sql = "SELECT Subject_Category, Subject_Category2 FROM $tableName";
    $result = $conn->query($sql);

    $data = array();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            // Each entry will be a JSON object with both Subject_Category and Subject_Category2
            $data[] = array(
                "Subject_Category" => $row['Subject_Category'],
                "Subject_Category2" => $row['Subject_Category2']
            );
        }
    }
    echo json_encode($data); // Send data as JSON
} else {
    echo json_encode(array("error" => "Table does not exist"));
}

$conn->close();
?>
