-1
<?php
include("WIP/dbconnect.php")
?>
</head>

<?php
$link = Connection();
$result=mysql_query("SELECT `testLocation`.`Date`, 
    `testLocation`.`Serial`, `testLocation`.`State`, 
    `testLocation`.`Count` FROM `testLocation`",$link);

if($result!==FALSE){
    //create an array
    $emparray = array();
    while($row = mysql_fetch_array($result))
    {
        $emparray[] = $row;
    }
    echo json_encode($emparray);

    mysql_free_result($result);
    mysql_close();
}
?>

I am currently trying to convert some data from my mysql database into json format but for some reason the data being produced isn't in a good format. It looks like this Data Link

I got the JSON data to format correctly but I am unable to generate the graph. I am currently using AmCharts.com to generate the graph. It says Cannot Parse Json

  <script>

  var chart = AmCharts.makeChart( "chartdiv", {
    "type": "serial",
    "dataLoader": {
      "url": "index2.php"
    },
    "pathToImages": "http://www.amcharts.com/lib/images/",
    "categoryField": "Date",
    "dataDateFormat": "YYYY-MM-DD 00:00:00",
    "startDuration": 1,
    "categoryAxis": {
      "parseDates": true
    },
    "graphs": [ {
      "valueField": "Serial",
      "bullet": "round",
      "bulletBorderColor": "#FFFFFF",
      "bulletBorderThickness": 2,
      "lineThickness ": 2,
      "lineAlpha": 0.5
    }, {
      "valueField": "State",
      "bullet": "round",
      "bulletBorderColor": "#FFFFFF",
      "bulletBorderThickness": 2,
      "lineThickness ": 2,
      "lineAlpha": 0.5
    }, {
      "valueField": "Count",
      "bullet": "round",
      "bulletBorderColor": "#FFFFFF",
      "bulletBorderThickness": 2,
      "lineThickness ": 2,
      "lineAlpha": 0.5
    }]   } );
amiao
  • 21
  • 5
  • [Is this what you're trying to do?](http://stackoverflow.com/questions/6054033/pretty-printing-json-with-php) – Marty Mar 15 '16 at 00:02
  • I actually got the JSON to work, it validates but I am unable to graph it using amCharts, it says unable to Parse – amiao Mar 15 '16 at 00:18
  • Does `makeChart` expect a JSON string or an object? – Marty Mar 15 '16 at 00:19
  • From what I'm reading on the documentation it is makeChart(container, config, delay) The div tag, and the configuration – amiao Mar 15 '16 at 00:25

1 Answers1

1

If you use mysql_fetch_assoc() instead of mysql_fetch_array() you will just get only an assoc array and not an assoc array and a numeric array together.

while($row = mysql_fetch_assoc($result)) {
    $emparray[] = $row;
}
echo json_encode($emparray);

If you were to use mysql_fetch_object() then you could easily generate an array of objects like this

while($row = mysql_fetch_object($result)) {
    $emparray[] = $row;
}
echo json_encode($emparray);

Manual mysql_fetch_assoc()

Manual mysql_fetch_array()

Manual mysql_fetch_object()

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I changed it to fetch_object and it looks better, but they are appearing side by side, how do I have it appear line by line? – amiao Mar 14 '16 at 22:50
  • Sorry I am not sure I understand your question – RiggsFolly Mar 14 '16 at 22:52
  • Remember JSON is data represented as a STRING. There is no visual formatting in a JSON String. – RiggsFolly Mar 14 '16 at 22:54
  • Alright so I validated the JSON data but for some reason it won't parse. I am currently trying to use amCharts.com to create the graph. I posted the code for the amCharts above. – amiao Mar 15 '16 at 00:12
  • I am afraid that constitutes a second question. You will have to ask that as another question, or it is going to get lost – RiggsFolly Mar 15 '16 at 01:35