<?php
	header('Access-Control-Allow-Origin: *');

	include('_global.php');

	if( isset( $_REQUEST['table'] ) )
	{
		$table = $_REQUEST['table'];

		$columns = (isset($_REQUEST["columns"])?$_REQUEST["columns"]:"*");

		$where = $_REQUEST['where'];

		$group = $_REQUEST['group'];

		$order = $_REQUEST['order'];

		$records = [];

		if($table>'')
		{
			$sql = "SELECT $columns FROM $table";

			if($where>'') $sql .= " WHERE $where";

			if($group>'') $sql .= " GROUP BY $group";

			if($order>'') $sql .= " ORDER BY $order";

			$return = mysql_query($sql, $conn);
			$message = mysql_error($conn);
			if($message == '')
			{
				while($record = mysql_fetch_assoc($return)) array_push($records,$record);
			}
		}
		$num = count($records);
	}
	else
	{
		$message = "Table not defined";
	}
	
	$json = json_encode([
			"message" => $message, 
			"columns" => $_REQUEST["columns"], 
			"table" => $_REQUEST["table"], 
			"where" => $_REQUEST["where"], 
			"group" => $_REQUEST["group"], 
			"order" => $_REQUEST["order"], 
			"n" => $num, 
			"records" => $records 
		], JSON_PRETTY_PRINT);

	echo $json;

?>