这个问题在这里已有答案:
我有不同的选项卡,应显示不同的图表。第一个选项卡显示第一个图表没有问题,但我在第二个选项卡中显示第二个图表时遇到问题。当我点击第二个标签时,我只是继续收到这些错误:
Warning: mysqli::query(): Couldn't fetch mysqli in C:\xampp\htdocs\palo\graph.php on line 248
Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\palo\graph.php on line 248
Warning: main(): Couldn't fetch mysqli in C:\xampp\htdocs\palo\graph.php on line 248
Error code ():
这是我的代码:
<div id="c2017" class="tabcontent">
<center>
<?php
$strQuery = "SELECT COUNT(*) as student_count, exam_year, result FROM studentsinfo WHERE exam_year = 2017 GROUP BY result ORDER BY student_count DESC";
$result = $dbhandle->query($strQuery) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");
if ($result) {
$arrData = array(
"chart" => array(
"caption" => "TEST RESULTS GRAPH FOR SCHOOL YEAR 2017",
"showValues" => "0",
"theme" => "fint"
)
);
$arrData["data"] = array();
while($row = mysqli_fetch_array($result)) {
array_push($arrData["data"], array(
"label" => $row["result"],
"value" => $row["student_count"]
)
);
}
$jsonEncodedData = json_encode($arrData);
$columnChart = new FusionCharts("column2D", "myFirstChart" , 700, 400, "chart-1", "json", $jsonEncodedData);
$columnChart->render();
$dbhandle->close();
}
?>
<br>
<div id="chart-1"></div>
</center>
</div>
<div id="c2018" class="tabcontent">
<center>
<?php
$strQuery2 = "SELECT COUNT(*) as student_count, exam_year, result FROM studentsinfo WHERE exam_year = 2018 GROUP BY result ORDER BY student_count DESC";
$result2 = $dbhandle->query($strQuery2) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");
if ($result2) {
$arrData2 = array(
"chart" => array(
"caption" => "TEST RESULTS GRAPH FOR SCHOOL YEAR 2018",
"showValues" => "0",
"theme" => "fint"
)
);
$arrData2["data"] = array();
while($row2 = mysqli_fetch_array($result2)) {
array_push($arrData2["data"], array(
"label" => $row2["result"],
"value" => $row2["student_count"]
)
);
}
$jsonEncodedData2 = json_encode($arrData2);
$columnChart2 = new FusionCharts("column2D", "mySecondChart" , 700, 400, "chart-2", "json", $jsonEncodedData2);
$columnChart2->render();
$dbhandle->close();
}
?>
<br>
<div id="chart-2"></div>
</center>
</div>
错误在某处:
$strQuery2 = "SELECT COUNT(*) as student_count, exam_year, result FROM studentsinfo WHERE exam_year = 2018 GROUP BY result ORDER BY student_count DESC";
$result2 = $dbhandle->query($strQuery2) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");
我不知道错误意味着什么,因为我还是新手。我希望有人可以看看这个问题,并帮我修复我的代码。非常感谢!
好吧,你在第一个图表后关闭$dbhandle
,所以它和mysqli连接不能用于第二个查询。
在所有查询完成之前保持打开状态。因此,仅在最后一次查询后使用$dbhandle->close();
。