简体   繁体   English

Google图表日期时间问题,现在不显示JSON数据

[英]Google Charts datetime issue, not displaying JSON data now

Basically it doesn't seem like my select statement is working when I try to generate this Google Chart. 基本上,当我尝试生成此Google图表时,我的select语句似乎不起作用。 The echo statements below output nothing, like there's no selection being made. 下面的echo语句不输出任何内容,就像没有选择一样。 I've been staring at this long enough, that I know the error is there, I'm just not able to see it at this point. 我已经凝视了足够长的时间,以至于我知道错误在那里,但现在还看不到它。 I initially had a problem where I was only able to process the timestamped data as a string, but since implementing the code below, it's like it's not selecting anything. 最初我遇到一个问题,即我只能将带有时间戳的数据作为字符串进行处理,但是由于实现了下面的代码,因此好像什么也没选择。 I'm not getting any errors, but when I echo out the JSON data and the query, the JSON data rows are empty and the SQL query looks properly formatted. 我没有收到任何错误,但是当我回显JSON数据和查询时,JSON数据行为空,SQL查询的格式正确。

This is what I get back with the echo statements: 这就是我用echo语句得到的结果:

{"cols":[{"id":"Date","label":"Date","type":"datetime"},{"id":"Usage","label":"Usage","type":"number"}],"rows":[]} SELECT DATE(`dateRead`) as day, SUM(Ch3)*0.008 as totals FROM SNdata WHERE (dateRead >= 2012-01-01 AND dateRead <= 2012-01-15) and sn=5018 GROUP BY day

Code is below, as well as the database create code, and some sample data. 代码在下面,以及数据库创建代码和一些示例数据。

Thanks. 谢谢。

Can anybody see where this is going wrong? 有人可以看到这是怎么回事吗? I might just be too tired to see what's going wrong here. 我可能只是太累了,看不到这里出了什么问题。 I could really use a nudge in the right direction. 我真的可以朝正确的方向轻推。

Also, keep in mind that this application will be run offline everytime and I have zero concerns about SQL injection. 另外,请记住,此应用程序每次都将脱机运行,而我对SQL注入的担忧为零。

<?php
$serialNum = '5018';
$Chan = '3';
$Mult = '0.008';
$startDate = '2012-01-01';
$endDate ='2012-01-15';

$con=mysql_connect("localhost","user","password") or die("Failed to connect with database!!!!");
mysql_select_db("mydb", $con); 
$selectSQL="SELECT
                DATE(`dateRead`) as day,
                SUM(Ch$Chan)*$Mult as totals
            FROM SNdata
            WHERE (dateRead >= $startDate AND dateRead <= $endDate) AND sn=$serialNum
            GROUP BY day";
$sth = mysql_query($selectSQL); 
    $data = array (
        'cols' => array( 
            array('id' => 'Date', 'label' => 'Date', 'type' => 'datetime'), 
            array('id' => 'Usage', 'label' => 'Usage', 'type' => 'number')
    ),
        'rows' => array()
);
while ($res = mysql_fetch_assoc($sth)) {
// assumes dates are patterned 'yyyy-MM-dd hh:mm:ss'
preg_match('/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/', $res['day'], $match);
$year = (int) $match[1];
$month = (int) $match[2] - 1; // convert to zero-index to match javascript's dates
$day = (int) $match[3];
$hours = (int) $match[4];
$minutes = (int) $match[5];
$seconds = (int) $match[6];
array_push($data['rows'], array('c' => array(
    array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)"), 
    array('v' => $res['totals'])
)));

}
echo json_encode($data, JSON_NUMERIC_CHECK);

echo  $selectSQL;
?>
<html>
  <head>
    <!--Load the Ajax API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
       // Load the Visualization API and the chart package.
    google.load('visualization', '1', {'packages':['corechart']});
    google.setOnLoadCallback(drawChart);
    function drawChart() {

      var data = new google.visualization.DataTable(<?php echo json_encode($data); ?>);
      var options = {
        title: 'I am a potato',
        hAxis: {
          title: 'Date',
        },
        vAxis: {
          title: 'Useage',
        }
      };
      var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>
  <body>    
    <div id="chart_div"></div>
  </body>
</html>

Here's how the database is set up 这是建立数据库的方式

CREATE TABLE `SNdata` (
    `SN` INT(10) UNSIGNED NULL DEFAULT '0',
    `dateRead` DATETIME NULL DEFAULT NULL,
    `Ch1` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    `Ch2` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    `Ch3` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    `Ch4` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    `Ch5` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    `Ch6` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    `Ch7` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    `Ch8` MEDIUMINT(8) UNSIGNED NULL DEFAULT '0',
    INDEX `DateINX` (`dateRead`),
    INDEX `SNINX` (`SN`)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM;

Here's an example of how the database is set up 这是一个如何建立数据库的例子

5018,01/01/2012 00:15,0,77,73,84,0,0,3,62
5018,01/01/2012 00:30,0,100,45,77,0,0,3,67
5018,01/01/2012 00:45,0,96,62,73,0,0,2,61
5018,01/01/2012 01:00,0,81,79,85,0,0,3,56
5018,01/01/2012 01:15,0,79,47,73,0,0,2,45
5018,01/01/2012 01:30,0,72,54,69,0,0,3,65
5018,01/01/2012 01:45,0,121,100,77,0,0,3,61
5018,01/01/2012 02:00,0,77,52,87,0,0,2,61
5018,01/01/2012 02:15,0,100,99,87,0,0,3,61
5018,01/01/2012 02:30,0,87,110,107,0,0,2,60
5018,01/01/2012 02:45,0,91,86,79,0,0,3,69
5018,01/01/2012 03:00,0,91,75,67,0,0,3,61
5018,01/01/2012 03:15,0,93,99,77,0,0,2,66
5018,01/01/2012 03:30,0,87,97,72,0,0,3,64
5018,01/01/2012 03:45,0,89,73,80,0,0,3,55
5018,01/01/2012 04:00,0,92,80,69,0,0,2,46
5018,01/01/2012 04:15,0,83,94,84,0,0,3,54
5018,01/01/2012 04:30,0,99,81,66,0,0,2,67
5018,01/01/2012 04:45,0,77,89,65,0,0,3,60
5018,01/01/2012 05:00,0,85,85,79,0,0,3,

Your SQL is fundamentally broken. 您的SQL从根本上被破坏了。 You're inserting date values directly into the query without quoting: 您正在将日期值直接插入查询中而不引用:

WHERE (dateRead >= $startDate AND dateRead <= $endDate) ...

which becomes 变成

WHERE (dateRead >= 2012-01-11 AND dateRead <= 2012-01-15) ...

Since they're not quoted, they're just arithemetic expressions: You're doing SUBTRACTION, not dates, so you're effectively running 由于它们没有被引用,所以它们只是算术表达式:您正在执行SUBTRACTION,而不是日期,因此您可以有效地运行

WHERE (dateRead >= 2011 AND dateRead <= 1996)

and your query returns nothing, because nothing matches. 您的查询不会返回任何内容,因为没有匹配项。

YOu shouldn't be using mysql_*() functions anymore, and you should NOT be directly dumping values into a query string, but if you insist on continuing this way, then at least have 您不应该再使用mysql _ *()函数,并且您不应该直接将值转储到查询字符串中,但是如果您坚持以这种方式继续,那么至少

WHERE (dateRead >= '$startDate' AND dateRead <= '$endDate') ...

note the ' -quotes... 注意'引号...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM