Chartjs-Use PHP to populate chart.js with data from an SQLite Database

0👍

You can do something like this:

getData.php:

$dir = 'sqlite:/home/pi/AQUASOLAR/SHT30/log_db/db.sht.6.db';
$dbh = new PDO($dir) or die("cannot open the database");
$query = "SELECT * FROM READING");
$r0 = Array();
foreach ($dbh->query($query) as $row) {
  $r0 = $row[0];
}
$dbh = null;
echo json_encode($r0);

index.html

<!DOCTYPE html>
<html lang="en">
  ..
  <body>

    <script>
      // include jquery
      $.ajax({
        url: './getData.php',
        type: 'GET',
        success: (data) => {
          const parsed = JSON.parse(data);
          // Use parsed to create your chart
        },
        error: (err) => {
          alert(err);
        }
      });
    </script>
  </body>
</html>

This is just an example of the logic that I use to resolve the problem. Maybe there are some sintax error

Leave a comment