<?php
// get these values from your DB.
require_once('config.php');
$error_message;
$valid = true;

//$text={"site_cd":"B05","EmpID":"DEMO","EmpName":"DEMO"};




$sql_priority_date="select * from wrk_pri ";
			
	
		
$stmt_priority_date = sqlsrv_query( $conn, $sql_priority_date);
	
if( !$stmt_priority_date ) {
     $error_message = "Error selecting HWMS asset criticality (wrk_pri)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
	 
}

$json= array();

	do {
		while ($row = sqlsrv_fetch_array($stmt_priority_date, SQLSRV_FETCH_ASSOC)) {	
		
		$json[] = $row;		

		}
	} while ( sqlsrv_next_result($stmt_priority_date) );
	sqlsrv_free_stmt( $stmt_priority_date);	



if ($valid) {

json_validate($json);


	
	//echo $sql;
	returnData($json);	
	
	sqlsrv_close( $conn);	
	
}

function returnData($json){
	
	echo 'sucess';
	$returnData = array(
	'status' => 'Update Alert',
	'message' => 'New version available Please, update app to New  Version   : .',
	'json' => $json);
	
	
	echo json_encode($returnData);	
	

	
}

function returnError($error_message){
	
	echo 'error';
	$json = array();
	
	$returnData = array(
	
	'message' => $error_message,
	'data' => '');
	
	
	echo json_encode($returnData);	
	exit();	
		
}


function json_validate($string)


{
	
	echo 'in';
	
    // decode the JSON data
    $result = json_decode($string);

    // switch and check possible JSON errors
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            $error = ''; // JSON is valid // No error has occurred
            break;
        case JSON_ERROR_DEPTH:
            $error = 'The maximum stack depth has been exceeded.';
            break;
        case JSON_ERROR_STATE_MISMATCH:
            $error = 'Invalid or malformed JSON.';
            break;
        case JSON_ERROR_CTRL_CHAR:
            $error = 'Control character error, possibly incorrectly encoded.';
            break;
        case JSON_ERROR_SYNTAX:
            $error = 'Syntax error, malformed JSON.';
            break;
        // PHP >= 5.3.3
        case JSON_ERROR_UTF8:
            $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
            break;
        // PHP >= 5.5.0
        case JSON_ERROR_RECURSION:
            $error = 'One or more recursive references in the value to be encoded.';
            break;
        // PHP >= 5.5.0
        case JSON_ERROR_INF_OR_NAN:
            $error = 'One or more NAN or INF values in the value to be encoded.';
            break;
        case JSON_ERROR_UNSUPPORTED_TYPE:
            $error = 'A value of a type that cannot be encoded was given.';
            break;
        default:
            $error = 'Unknown JSON error occured.';
            break;
    }

    if ($error !== '') {
        // throw the Exception or exit // or whatever :)
        exit($error);
    }

    // everything is OK
    return $result;
}
	

 
?>