<?php

$response = array();

define('DB_USER', "abomesbah_system"); // db user
define('DB_PASSWORD', "1adAbvX1Bp]j"); // db password (mention your db password here)
define('DB_DATABASE', "abomesbah_system"); // database name
define('DB_SERVER', "localhost"); // db server

date_default_timezone_set('Asia/Bahrain');


$mysqli = new mysqli(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE);

mysqli_query($mysqli,"SET CHARACTER SET 'utf8'");
mysqli_query($mysqli,"SET SESSION collation_connection ='utf8_unicode_ci'");


if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}




header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');


// get all products from products table
// Start Functionality  

if (isset($_GET['query']) && !empty($_GET['query'])) {
    $query = $_GET['query'];
    

   


$result = mysqli_query($mysqli,$query) or die(mysql_error());
// check for empty result
    if (mysqli_num_rows($result) > 0) {
        // looping through all results
        // products node
        $product= array();
        
        
        while ($row = mysqli_fetch_array($result)) {

            // temp user array

            $product[] = $row;

            // push single product into final response array
        }
        // success
        $response["success"] = 1;
        echo json_encode($product);

    } else {

        $response["product"] = array();

        // temp user array
        $product = array();

        // success
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "هناك بيانات مفقوده برجاء مراجعة بياناتك";
    
    // echo no users JSON
    echo json_encode($response);
}
?>