从日期SQL PHP提取小时

时间:2016-11-18 18:45:16

标签: php mysql

大家好我试图从mySQL上的timestamp列中提取小时数。我在做

时遇到错误
SELECT EXTRACT(HOUR, time) FROM sensor; 
  

警告:   mysql_fetch_assoc()期望参数1是资源,布尔值在..

中给出
$sth = mysql_query("SELECT EXTRACT(HOUR, [time]) FROM sensor");

$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(

    // Labels for your chart, these represent the column titles
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
    array('label' => 'Date', 'type' => 'string'),
    array('label' => 'Acum Energy', 'type' => 'number')

);

$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r['time']); 

    // Values of each slice
    $temp[] = array('v' => (float) $r['value']); 
    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;
?>

我试图在谷歌图表上绘制这个。它适用于完整的日期。但我只想显示HOUR

1 个答案:

答案 0 :(得分:1)

proper syntaxEXTRACT (unit FROM date),因此请将您的查询更改为:

SELECT EXTRACT(HOUR FROM `time`) FROM sensor

如果您遇到语法或其他问题,请务必检查错误,而不是盲目地假设它有效。