-3

Hello guys I'm trying to extract hours from a timestamp column on mySQL. I get an error while doing

SELECT EXTRACT(HOUR, time) FROM sensor; 

WARNING: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in ..

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

Im trying to plot this on a google chart. It works with the complete date.But i just want to display the HOUR

Qirel
  • 23,315
  • 7
  • 41
  • 57
  • 1) Check for errors instead of blindly assuming that it works. You may be getting a syntax error. 2) mysql_* functions have been removed in PHP7 and deprecated in previous versions. Prevent headaches by switching to PDO or mysqli. – aynber Nov 18 '16 at 18:47

1 Answers1

1

The proper syntax is EXTRACT (unit FROM date), so change your query to this:

SELECT EXTRACT(HOUR FROM `time`) FROM sensor

Always check for errors in case you run into syntax or other issues, instead of blindly assuming that it works.

aynber
  • 20,647
  • 8
  • 49
  • 57