<?php
require_once('config.php');
//require_once('f_update_budget.php');

$error_message;
$valid = true;


/* Begin the transaction. */
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}


$tkn_site = $_REQUEST['tkn_site'];
$tkn_dvc_id = $_REQUEST['tkn_dvc_id'];
$tkn_user = $_REQUEST['tkn_user'];

$sql= "select   tkn_dvc_id,
				tkn_brand,
				tkn_model,
				tkn_token  = COALESCE(tkn_token,''),
				tkn_user,
				tkn_site,
				tkn_login,
				tkn_logout,
				tkn_online,
				RowID from tkn_mst (NOLOCK) 
		where tkn_site = '".$tkn_site."' 
		and tkn_dvc_id = '".$tkn_dvc_id."' 
		and tkn_user = '".$tkn_user."' and tkn_online= 1";

$stmt = sqlsrv_query( $conn, $sql);

if( !$stmt ) {
     $error_message = "Error selecting table (tkn_mst)";
	 returnError($error_message);
     die( print_r( sqlsrv_errors(), true));
}

$json = array();

do {
     while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
     $json[] = $row;
     }
} while ( sqlsrv_next_result($stmt) );

if ($valid) {
			
	returnData($json);	
	sqlsrv_free_stmt( $stmt);
	sqlsrv_close( $conn);
}

function returnData($json){
	$returnData = array(
	'status' => 'SUCCESS',
	'message' => 'Successfully',
	'data' => $json);
	
	echo json_encode($returnData);
}

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