0

I'm trying to draw a pie chart using PHP and MySQL, but I don't see anything, any error just a blank page, I don't know where is the problem exactly, my query is correct. Any help, any suggestion! Thank you. This is my code that I'm using:

<?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>
8
  • Give the count an alias Commented May 9, 2020 at 21:11
  • @strawberry you mean rename count(ID_etudiant) ?? Commented May 9, 2020 at 21:14
  • @strawberry i did $sql="SELECT Nom_matiere, COUNT(ID_etudiant) as ID FROM note,matiere WHERE note>=12 and matiere.Num_matiere=note.Num_matiere GROUP BY note.Num_matiere"; then i changed echo "['".$results['Nom_matiere']."',".$results['COUNT(ID_etudiant)']."],"; with echo "['".$results['Nom_matiere']."',".$results['ID']."],"; but nothing changed !! Commented May 9, 2020 at 21:16
  • I find this almost illegible :-( Commented May 9, 2020 at 21:23
  • i told you I rename count(ID_etudiant) , count(ID_etudiant) as ID as you told me but i get the same error @strawberry Commented May 9, 2020 at 22:09

1 Answer 1

1

It worked for me: I added this line $result=$sth->fetchAll(); and I added a loop foreach, the code will be like this:

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

          ?>
Sign up to request clarification or add additional context in comments.

3 Comments

Cool. FWIW, I find this easier to read: echo "['{$row['Nom_matiere']}',{$row['id']}],";
Also, it doesn't make sense to GROUP BY a different column from the one that is selected - Nom_matiere <> note.num_matiere
And always write your JOINs using explicit JOIN syntax. We stopped using comma joins in 1992. And qualify ALL column names !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.