2016-11-18 70 views
-3

你好傢伙我想從mySQL上的時間戳列提取小時。我得到一個錯誤,而這樣做從日期SQL提取小時PHP

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; 
?> 

我試着繪製該上一個谷歌圖表。它的工作原理與完整date.But我只是想顯示小時

+0

1)檢查錯誤,而不是盲目地認爲它有效。您可能會收到語法錯誤。 2)mysql_ *函數已在PHP7中刪除,並在以前的版本中被棄用。通過切換到PDO或mysqli來防止頭痛。 – aynber

回答

1

proper syntaxEXTRACT (unit FROM date),所以更改您的查詢到這一點:

SELECT EXTRACT(HOUR FROM `time`) FROM sensor 

經常檢查,以防錯誤,你碰上的語法或其他問題,而不是盲目地認爲它有效。