I'm trying to store MySQL info into a JSON array so I can use it in Chart.js to refresh the chart without refreshing the page. I tried adding sensors info to an array but for some reason, it is only outputting the last info sent. My database has 3 columns with the name as sensor1, sensor2, sensor3. This is what I tried:
<?php
session_start();
if(!isset($_SESSION['usersId']))
{
header("Location: ../index.php");
exit();
}
else
{
include_once 'includes/dbh.inc.php';
}
$id = $_SESSION['userId'];
$dBname = "infosensor";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBname);
$sql = "SELECT sensor1, sensor2, sensor3 FROM `$id` ORDER BY id DESC LIMIT 1;";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$jsonsensor = array();
if ($row)
{
$jsonsensor[] = $row;
}
echo json_encode($jsonsensor);
?>
The output:
[{"sensor1":"5","sensor2":"5","sensor3":"0"}]