Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Use data from SQL database in .js file with PHP

I use chart.js on a page and I want to use data stored in a SQL database as data for chart.js

Here's what the chart.js snippet looks like:

// Chart Data
var chartData = {
    labels: ["Jänner", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
    datasets: [{
        label: "Wert Verkaufter Waren 2022 - Standorte kumuliert",


        //Data for Graph
        data: [65, 59, 80, 81, 56, 55, 40],


        fill: false,
        borderDash: [5, 5],
        borderColor: "#346200",
        pointBorderColor: "#346200",
        pointBackgroundColor: "#FFF",
        pointBorderWidth: 2,
        pointHoverBorderWidth: 2,
        pointRadius: 4,
    }]
};

The data values [65, 59, 80, 81, 56, 55, 40] is what i want to replace with data from my database.

Can I archive this with the use of php?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Of course you can achieve this with PHP.

There are two options how to do this:

  1. Add PHP code inline to your JavaScript data within your webpage.
  2. Create an extra PHP script that fetches the data from your database and outputs it as JSON. The JavaScript then can fetch the JSON from your extra PHP script.

The first method would be something like this. Please remember that this is a quick sketch that does not include error handling, security best practises, etc.

<?php
  $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
  $result = $mysqli->query("SELECT wert FROM verkaeufe_monatlich;");
  $data = $result->fetch_all();
  $mysqli->close();
?>
<html>
<!-- Your normal webpage html content -->
<script>
var chartData = {
    labels: ["Jänner", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
    datasets: [{
        label: "Wert Verkaufter Waren 2022 - Standorte kumuliert",


        //Data for Graph
        data: <?php echo json_encode($data); ?>,


        fill: false,
        borderDash: [5, 5],
        borderColor: "#346200",
        pointBorderColor: "#346200",
        pointBackgroundColor: "#FFF",
        pointBorderWidth: 2,
        pointHoverBorderWidth: 2,
        pointRadius: 4,
    }]
};
</script>
</html>

For the second method, create a separate PHP script that roughly contains the database logic and JSON encoding as above and call it from your script like this:

<script>
fetch('/database-query.php')
  .then(response => response.json())
  .then(jsonData => {
    const chartData = {
      labels: ["Jänner", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
      datasets: [{
        label: "Wert Verkaufter Waren 2022 - Standorte kumuliert",
        data: jsonData,
        fill: false,
        borderDash: [5, 5],
        borderColor: "#346200",
        pointBorderColor: "#346200",
        pointBackgroundColor: "#FFF",
        pointBorderWidth: 2,
        pointHoverBorderWidth: 2,
        pointRadius: 4,
      }]
    };
    // do stuff with chartData
  })
  .catch(error => {
    console.error(error);
  });
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!