简体   繁体   中英

Multiple Queries In One PHP Page

I need to execute multiple queries to form multiple graphs on a PHP (7.0.9) web page. When the page runs, I'm able to execute sqlsrv_query() once. When I attempt to execute it a second time, I receive this PHP error:

[13-Dec-2016 13:17:26 America/New_York] PHP Fatal error: Maximum execution time of 300 seconds exceeded in C:\\File\\File\\piechart.php on line 249

Line 249 is print_r(array_values($dataa)); For now, this is just a test to see how the variables output. Here's the entire code.

<?php
$connectionInfo = array(  "UID" => "form",  "PWD" => "-------",  "Database" => "-------");
$serverName = "--------";
$conn = sqlsrv_connect($serverName, $connectionInfo);
$test = 0;
ob_start();
if ($conn === false) die("<pre>".print_r(sqlsrv_errors(), true));
echo "Successfully connected!";
ob_end_clean();
$sql  = "SELECT Defect, COUNT(*) AS Amount FROM dbo.InputData Where Defect IS NOT NULL GROUP BY Defect ";
$sql2  = "SELECT Comments, COUNT(*) AS Amount FROM dbo.InputData WHERE Comments LIKE '%wav%' OR Comments LIKE '%way%' GROUP BY Comments";
$stmt = sqlsrv_query( $conn, $sql);
if( ($errors = sqlsrv_errors() ) != null) {
        foreach( $errors as $error ) {
            echo "SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
            echo "code: ".$error[ 'code']."<br />";
            echo "message: ".$error[ 'message']."<br />";
        }
    }
?>

<!DOCTYPE html>
<!-- Bunch of HTML code here... -->
<table width="100%">
    <tr>
        <td>
        <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([
                  ['Defect', 'Ammount'],
                          <?php while($data = sqlsrv_fetch_array($stmt)) {
                  echo "['" . $data[0]. "', " . $data[1] . "], 
                  ";
                } ?>
                ]);

                var options = {
                  title: 'Glass Defects',
                  is3D: true,
                };

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

                chart.draw(data, options);
              }
            </script>
            <div id="piechart" style="width: 900px; height: 500px;"></div>
        </td>
        <td>
        <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([
                  ['Defect', 'Ammount'],
                    <?php 
                            while($dataa = sqlsrv_fetch_array(sqlsrv_query( $conn, $sql2))) {
                            print_r(array_values($dataa)); //Line 249
                            } 
                    ?>
                ]);

                var options = {
                  title: 'Glass Defects',
                  is3D: true,
                };

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

                chart.draw(data, options);
              }
            </script>
        </td>
    </tr>
</table>
<!-- Bunch more HTML... -->
</html>

You're running the query again every time you start the while loop. As a result, it runs an infinite number of times, causing your script to time out (like the error says). Instead of this:

while($dataa = sqlsrv_fetch_array(sqlsrv_query( $conn, $sql2))) {
    print_r(array_values($dataa)); //Line 249
} 

Do this:

$result = sqlsrv_query( $conn, $sql2);
while($dataa = sqlsrv_fetch_array($result)) {
    print_r(array_values($dataa)); //Line 249
} 

Also, you really might want to separate the various parts of your code. Mixing HTML, PHP, and JavaScript is a recipe for a maintenance nightmare, and it makes properly testing your application almost impossible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM