使用php和mysql绘制饼图

问题描述 投票:0回答:1

我正在尝试使用PHP和MySQL绘制饼图,但是我什么也没看到,任何错误都只是空白页,我不知道问题出在哪里,我的查询是正确的。任何帮助,任何建议!谢谢。这是我正在使用的代码:

<?php
    $dsn='mysql:host=localhost;dbname=tp3_php';
    $user='root';
    $pass='';
    try {
      $bdd = new PDO($dsn,$user,$pass);
    } catch (Exception $e) {
      die('Erreur : ' . $e->getMessage());
    }    
    $sql="
    SELECT Nom_matiere
         , COUNT(ID_etudiant)  
      FROM note
         , matiere 
     WHERE note>=12 
       and matiere.Num_matiere = note.Num_matiere 
     GROUP 
        BY note.Num_matiere
    ";
            $sth = $bdd->query($sql);
?>
<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Nom_matiere', 'taux de reussis'],
          <?php

              while ($result=$sth->fetchAll()) 
              {

      echo "['".$results['Nom_matiere']."',".$results['COUNT(ID_etudiant)']."],";
              }

          ?>

        ]);

        var options = {
          title: 'Taux de réussite des étudiants par module'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart" style="width: 900px; height: 500px;"></div>
  </body>
</html>
php mysql pie-chart
1个回答
0
投票

对我有用:我添加了这一行$result=$sth->fetchAll();,并添加了一个循环foreach,代码将如下所示:

['Nom_matiere', 'taux de reussis'],
          <?php
              $result=$sth->fetchAll();
            foreach ($result as $row) { 
              echo "['".$row['Nom_matiere']."',".$row['COUNT(ID_etudiant)']."],";
            }

          ?>
© www.soinside.com 2019 - 2024. All rights reserved.