-1

I have different tabs that should display different Charts. The first tab displays the first chart with no problem but I'm having a problem in displaying the second chart in the second tab. When I click on the second tab, I just keep on getting these errors:

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 ():

Here are my codes:

        <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>

The error is somewhere in:

 $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}");

I dont know what the error means, since I'm still new to this. I hope someone can take a look with this problem and help me fix my codes. Thank you so much!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Dizza
  • 53
  • 1
  • 2
  • 9

1 Answers1

0

Well, you close $dbhandle after the first chart, so it and the mysqli connection is not available for the second query.

Leave it open until all queries are done. So, use $dbhandle->close(); only after the last query.

Geshode
  • 3,600
  • 6
  • 18
  • 32