<?php
//define("DB_HOST", "51.81.151.210"); 
define("DB_HOST", "localhost");
define("DB_USERNAME", "easysoft_amir2");
define("DB_PASSWORD", "%?pyC=[s~&Gl");
define("DB_DATABASE_NAME", "easysoft_easysoft2");

//header("Access-Control-Allow-Origin: header");



$con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME);

if ($con->connect_error) {
    die("Connection Failed: " + $con->connect_error);
}

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

$uri = explode('/', $uri);

$requestMethod = $_SERVER['REQUEST_METHOD'];

function getQueryStringParams()
{
    return parse_str($_SERVER['QUERY_STRING'], $query);
}

$arrQueryStringParams = getQueryStringParams();

if ($_GET['query'] != null) {

    $sql = $_GET['query'];

    $sql = strval($sql);

    $sqlArr = explode(" ", $sql);

    // echo strtolower($sqlArr[0]);

    if (strtolower($sqlArr[0]) == "select") {
        $result = $con->query($sql);

        // echo $result . "<br>";

        // echo "Number of rows found: " . $result->num_rows . "<br />";

        $rows = $result->fetch_all(MYSQLI_ASSOC);

        echo json_encode($rows);

        return json_encode($rows);
    } else {
        if (strtolower($con->query($sql)) == TRUE) {
            echo "Query Performed SuccessFully";
        } else {
            echo "Case-sensitive or syntax error in parameter query: " . $con->error;
        }
    }
}
