【发布时间】:2023-03-20 23:50:02
【问题描述】:
我正在尝试使用 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>
【问题讨论】:
-
给计数起别名
-
@strawberry 你的意思是重命名 count(ID_etudiant) ??
-
@strawberry 我做了
$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";然后我改变了echo "['".$results['Nom_matiere']."',".$results['COUNT(ID_etudiant)']."],";和echo "['".$results['Nom_matiere']."',".$results['ID']."],";但没有任何改变!! -
我觉得这几乎难以辨认:-(
-
我告诉过你我重命名 count(ID_etudiant) , count(ID_etudiant) 为 ID,就像你告诉我的那样,但我得到了同样的错误@strawberry