我想使用PHP从html请求一个datetime变量,

并使用该变量从mysql数据库中选择数据。然后创建一个json来生成图表。

它成功生成json,但未生成highchart。

我用Firebug说TypeError:obj为null

这是我的代码
的PHP

<?php
   header('Content-type: application/json');
   $con = mysql_connect("localhost","root","");
   mysql_select_db("project", $con);
    $month=$_REQUEST['select_month'];

   $sqlChart_Q="SELECT T,value FROM `Temperature`WHERE month(T)='$month'";
   $sqlChart_R=mysql_query($sqlChart_Q);
   while($arrChart_ROW=mysql_fetch_array($sqlChart_R)) {
        $time=strtotime($arrChart_ROW[0])*1000;
          $temp=(float)$arrChart_ROW[1];
   $arrChart[]=array("time" => $time,"value" => $temp);
    }
    echo json_encode($arrChart);
     ?>


和js

     var chart;
     $(document).ready(function() {
     var options = {
        chart: {
           renderTo: 'month',
           type: 'spline',
        },
        title: {
        },
        xAxis: {
           type: 'datetime'

        },
        yAxis: {
        },
        series: [{
           name: 'Temperature',
           data: []
        }]
        };
        $.getJSON('PHP/db_month.php', function(json) {
        temp = [];
        $.each(json, function(key,value) {
        temp.push([value.time,value.value]);
              });
        options.series[0].data = temp;
        chart = new Highcharts.Chart(options);
        });
        });


json(select_month = 5; $ month = $ _ REQUEST ['select_month'])

[{"time":1400076317000,"value":50},{"time":1400076322000,"value":25},{"time":1400076327000,"value":34},{"time":1400110911000,"value":50},{"time":1400110916000,"value":43},{"time":1400110919000,"value":75},{"time":1400110920000,"value":35},{"time":1400110922000,"value":46},{"time":1400110924000,"value":66},{"time":1400110925000,"value":73},{"time":1400113566000,"value":20},{"time":1400161118000,"value":55},{"time":1400186496000,"value":50},{"time":1400193165000,"value":43},{"time":1400196532000,"value":50}]


顺便说一句,当sql函数更改为

    SELECT T,value FROM `Temperature`WHERE month(T)='5'


它将成功生成在5月创建数据的图表。

(PS:T的类型是时间戳,我不知道这没关系。

我的代码有什么问题?
有人可以帮我解决这个问题。
非常感谢你 !!

最佳答案

您需要在json_encode函数中设置JSON_NUMERIC_CHECK,因为您将返回字符串而不是数字。

09-25 19:56