Help to google charts

my chart not resive data see the site here:

$con = mysqli_connect('localhost','root','*****','db');
 <meta charset="utf-8">
 <script type="text/javascript" src=""></script>
  <script type="text/javascript" src=""></script>
    <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></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.

  // 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')


     $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',

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

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

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