I am following a tutorial to create a chart from my database.
https://www.element14.com/community/community/stem-academy/blog/2018/03/19/sensor-dashboard-for-raspberry-pi-using-mysql-php-and-highcharts
To get the data from the database, it provides the following code:
?php
$username=“username”;
$password=“password”;
$database=“database”;
$conn = mysqli_connect(“localhost”, $username, $password, $database);
if (mysqli_connect_error()) {
echo "Failed to connect to database: " . mysqli_connect_error();
exit();
}
$queryT1 = ‘SHOW Stamp, Temp1 FROM table’;
$resultT1 = mysqli_query($conn, $queryT1);
$dateTemp1 = array();
$index1 = 0;
while ($row1 = mysqli_fetch_array($resultT1, MYSQLI_NUM))
{
$dateTemp1[$index1]=$row1;
$index1++;
}
echo json_encode($dateTemp1, JSON_NUMERIC_CHECK);
mysql_close();
?>
This only returns
[]
without any of the data that I was expecting to see.
The connection to the database is good, and the table is populated, so I do not know why the returning page is empty?!
This data would then be used for a Highchart, which I believe is why it tries to prepare the data as an array, but I do not follow what is happening. Is there a better way?
The data gets called into the chart using html:
data: <?php echo json_encode($dateTemp1, JSON_NUMERIC_CHECK);?>
Cheers,
Sandy