Jump to content

JSON_ENCODE issues


Colton.Wagner

Recommended Posts

Hello All,

 

I am working on a project that pulls data from a mysql database and formatts it into json. It is encoded into json so that Google Charts API can plot the points for me. I had the code working previously but since I have updated the database I am having issues. I will list both sources of code below.

 

First this is the code that is working properly:

<?php
// Include functions file for ease of access
	require_once("./includes/functions.php");
	
// Connect to Mysql Database
	$mysql = new mysql;
	$mysql->connect();
	$mysql->database("Advance_Tek");

// Define Universal ID to query results.
	$uid = $_GET['uid'];
	$Sensor = $_GET['sensor'];
	
	$find = mysql_query("SELECT * FROM Device WHERE id='$uid'");
      		
    	if($result = mysql_fetch_array($find)){
      		$data[0] = array("Time", $result[$Sensor]);
      		$devicename = $result['Device_Name'];	
      	}
    
    
    switch ($_GET['action']) {
    	
    	case date_range:
    		$query = mysql_query("SELECT * FROM (SELECT * FROM Live_Data WHERE uid='$uid' AND Post_Date BETWEEN '$_GET[from]' AND '$_GET[to]' ORDER BY id DESC) tmp ORDER BY tmp.id ASC");
          	
        	if(!$query){
        		echo mysql_error();
        	}
        
        	$num = mysql_num_rows($query);
        
        	for ($i=1; $i<($num+1); $i++){
        		$data[$i] = array(mysql_result($query, $i-1, "Post_Time"), mysql_result($query, $i-1, $Sensor));
        	}
         	
        	echo json_encode($data, JSON_NUMERIC_CHECK);
    		
    		break;
    		
    	case time_range:
    		
    		//echo date_default_timezone_get();
    		date_default_timezone_set("US/Central");
    		$CurrentTime = date("H:i:s");
    		$CurrentDate = date("Y-m-d");
    		
    		list($CurrentHour, $CurrentMinutes, $CurrentSeconds) = explode(':', $CurrentTime);
    		
    		list($FromHour, $FromMinutes, $FromSeconds) = explode(':', $_GET['from']);
    		
    		
    		$StartTime = ($CurrentHour - $FromHour) . ":" . ($CurrentMinutes - $FromMinutes) . ":" . ($CurrentSeconds - $FromSeconds);
    		
    		//echo $StartTime . " To: " . $CurrentTime;
    		
    		$query = mysql_query("SELECT * FROM (SELECT * FROM Live_Data WHERE uid='$uid' AND Post_Date='$CurrentDate' AND Post_Time BETWEEN '$StartTime' AND '$CurrentTime' ORDER BY id DESC) tmp ORDER BY tmp.id ASC");
    		
    		if(!$query){
        		echo mysql_error();
        	}
        
        	$num = mysql_num_rows($query);
        
        	for ($i=1; $i<($num+1); $i++){
        		$data[$i] = array(mysql_result($query, $i-1, "Post_Time"), mysql_result($query, $i-1, $Sensor));
        	}
         	
        	echo json_encode($data, JSON_NUMERIC_CHECK);
    		
    		break;
    	
    	default:
    		$query = mysql_query("SELECT * FROM (SELECT * FROM Live_Data WHERE uid='$uid' ORDER BY id DESC LIMIT 20) tmp ORDER BY tmp.id ASC");
          	
        	if(!$query){
        		$content.= mysql_error();
        	}
        
        	$num = mysql_num_rows($query);
        
        	for ($i=1; $i<($num+1); $i++){
        		$data[$i] = array(mysql_result($query, $i-1, "Post_Time"), mysql_result($query, $i-1, $Sensor));
        	}
         	
        	echo json_encode($data, JSON_NUMERIC_CHECK);
   	
    }   

?>

Using the get method: json_live_data.php?uid=1&sensor=A00 it returns the following output.

[["Time","Flow & GPM"],["17:16:39",5.15],["17:21:39",5.08],["17:26:39",5.44],["17:31:39",4.87],["17:36:40",5.44],["17:41:40",6.15],["17:42:30",6.15],["17:46:42",5.78],["17:47:07",6.15],["12:09:50",4.36],["12:10:02",10.19],["12:10:13",10.19],["12:10:25",10.17],["12:10:37",10.1],["12:10:49",10.1],["12:11:01",10.06],["12:11:12",10.07],["12:11:24",10.06],["12:11:37",10.05],["12:11:49",10.15]]

This is the code that was working for me prior to updating. Below is the code that is currently giving me grief.

<?php
// Include functions file for ease of access
	require_once("./includes/functions.php");
	
// Connect to Mysql Database
	$mysql = new mysql;
	$mysql->connect();
	$mysql->database("Advance-Tek");

// Define Universal ID to query results.
	$uid = $_GET['uid'];
	$Sensor = $_GET['sensor'];
	
	$find = mysql_query("SELECT * FROM Device WHERE id='$uid'");
      		
    	if($result = mysql_fetch_array($find)){
      		$devicename = $result['Device_Name'];	
      	}
    
    $Sensor_Number = ltrim($Sensor,'CH_');  	
      	
    $ChannelConfig = mysql_query("SELECT * FROM Channel_Configuration WHERE id='" . $uid . "' AND Number='" . $Sensor_Number . "'");
    	
    	if($configuration = mysql_fetch_array($ChannelConfig)){
      		$data[0] = array("Time", $configuration['Name']);	
      	}
    
    
    switch ($_GET['action']) {
    	
    	case date_range:
    		$query = mysql_query("SELECT * FROM (SELECT * FROM Live_Data WHERE Device_ID='$uid' AND Date BETWEEN '$_GET[from]' AND '$_GET[to]' ORDER BY id DESC) tmp ORDER BY tmp.id ASC");
          	
        	if(!$query){
        		echo mysql_error();
        	}
        
        	$num = mysql_num_rows($query);
        
        	for ($i=1; $i<($num+1); $i++){
        		$data[$i] = array(mysql_result($query, $i-1, "Time"), mysql_result($query, $i-1, $Sensor));
        	}
         	
        	echo json_encode($data, JSON_NUMERIC_CHECK);
    		
    		break;
    		
    	case time_range:
    		
    		//echo date_default_timezone_get();
    		date_default_timezone_set("US/Central");
    		$CurrentTime = date("H:i:s");
    		$CurrentDate = date("Y-m-d");
    		
    		list($CurrentHour, $CurrentMinutes, $CurrentSeconds) = explode(':', $CurrentTime);
    		
    		list($FromHour, $FromMinutes, $FromSeconds) = explode(':', $_GET['from']);
    		
    		
    		$StartTime = ($CurrentHour - $FromHour) . ":" . ($CurrentMinutes - $FromMinutes) . ":" . ($CurrentSeconds - $FromSeconds);
    		
    		//echo $StartTime . " To: " . $CurrentTime;
    		
    		$query = mysql_query("SELECT * FROM (SELECT * FROM Live_Data WHERE Device_ID='$uid' AND Date='$CurrentDate' AND Time BETWEEN '$StartTime' AND '$CurrentTime' ORDER BY id DESC) tmp ORDER BY tmp.id ASC");
    		
    		if(!$query){
        		echo mysql_error();
        	}
        
        	$num = mysql_num_rows($query);
        
        	for ($i=1; $i<($num+1); $i++){
        		$data[$i] = array(mysql_result($query, $i-1, "Time"), mysql_result($query, $i-1, $Sensor));
        	}
         	
        	echo json_encode($data, JSON_NUMERIC_CHECK);
    		
    		break;
    	
    	default:
    		$query = mysql_query("SELECT * FROM (SELECT * FROM Live_Data WHERE Device_ID='$uid' ORDER BY id DESC LIMIT 20) tmp ORDER BY tmp.id ASC");
          	
        	if(!$query){
        		$content.= mysql_error();
        	}
        
        	$num = mysql_num_rows($query);
        
        	for ($i=1; $i<($num+1); $i++){
        		$data[$i] = array(mysql_result($query, $i-1, "Time"), mysql_result($query, $i-1, $Sensor));
        	}
         	
        	echo json_encode($data, JSON_NUMERIC_CHECK);
   	
    } 

?>

Using the GET method: json_live_data.php?uid=1&sensor=CH_1 it returns the following:

{"1":["15:05:54","343.324"],"2":["15:06:41","343.324"],"3":["15:08:22","343.324"],"4":["15:37:21","343.324"],"5":["15:43:17","343.324"],"6":["15:43:34","343.324"],"7":["15:51:52","352.861"],"8":["15:52:10","343.324"],"9":["15:52:28","1.953"],"10":["15:53:37","362.398"],"11":["16:02:58","3.906"],"12":["16:49:46","362.398"],"13":["12:57:25","343.324"],"14":["12:59:20","0.299"],"15":["12:59:56","0.299"],"16":["13:00:34","0.299"],"17":["13:01:11","0.299"],"18":["13:26:12","181.281"],"19":["13:26:49","181.302"],"20":["13:27:26","181.302"]}

Now I am trying to get Json_Encode to remove the Objects listed at the beggining with the current array number. Is there a simple fix for this issue any help would be greatly appreciated!

 

Thank you for your time!

Link to comment
https://forums.phpfreaks.com/topic/285562-json_encode-issues/
Share on other sites

I found the actual error:

$ChannelConfig = mysql_query("SELECT * FROM Channel_Configuration WHERE id='" . $uid . "' AND Number='" . $Sensor_Number . "'");
    
    	if(!$ChannelConfig){
    		die(mysql_error());
    	}
    
    	
    	if($configuration = mysql_fetch_array($ChannelConfig)){
      		$data[0] = array("Time", $configuration['Name']);	
      	}
      	
    echo $data[0][0] . $data[0][1];

the echo at the end should display the contents of the array. Being Time and whatever is queiered from the database. Instead it returns nothing. I resolved the issue because I needed to subtract one from the $Sensor_Number. Thanks for all who were interested.

Link to comment
https://forums.phpfreaks.com/topic/285562-json_encode-issues/#findComment-1466087
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.