Help to google charts

my chart not resive data see the site here:http://www.syndrom-event.net/chart.php

<?php
$con = mysqli_connect('localhost','root','*****','db');
?>
<!DOCTYPE HTML>
<html>
<head>
 <meta charset="utf-8">
 <title>TechJunkGigs</title>
 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<script type="text/javascript">

  // Load the Visualization API and the corechart package.
  google.charts.load('current', {'packages':['corechart']});

  // Set a callback to run when the Google Visualization API is loaded.
  google.charts.setOnLoadCallback(drawChart);

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
data.addColumn('string', 'edate');data.addColumn('number', 'belob')
   data.addRows([

<?php 

     $query = "SELECT edate, SUM(belob) FROM student WHERE id ='".$id."' GROUP BY edate ORDER BY edate";
      
      $exec = mysqli_query($con,$query);
 while($row = mysqli_fetch_array($exec)){
 
 echo "['".$row['edate']."',".$row['belob']."],";
      }
       
   ?>
  ]);

    // Set chart options
    var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':600,
                   'height':400};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

</head>
<body>
 <div class="container-fluid">
 <div id="chart_div" style="width: 100%; height: 500px;"></div>
 </div>
 
</body>
</html>

There’s no $id variable being set in the posted code, so the sql query is not matching anything. Where is $id supposed to come from?

Next, the above problem would be producing a php undefined variable error, assuming that your php error related settings are set up to either display or log all php errors. However, by putting the main php ‘business logic’, that knows how to get/produce data, inside javascript in the html document, any php errors would only appear in the ‘view source’ of the page. You should put the main php code before the start of your html document, fetch all the data from the query into an appropriately named php variable, then just use simple php code to test/loop over that php variable inside the html document. This will make writing, testing, and debugging your code easier.

Lastly, if the $id variable is an external/unknown value, use a prepared query instead of directly putting it into the sql query statement. You should also switch to the much simpler and more consistent PDO extension and should have error handling for all the database statements that can fail - connection, query, prepare, and execute.

Sponsor our Newsletter | Privacy Policy | Terms of Service