一个PHP页面中的多个查询

时间:2016-12-13 18:37:10

标签: php sql sql-server

我需要执行多个查询以在PHP(7.0.9)网页上形成多个图形。页面运行时,我可以执行sqlsrv_query()一次。当我第二次尝试执行它时,我收到了这个PHP错误:

  

[13-Dec-2016 13:17:26 America / New_York] PHP致命错误:第249行C:\ File \ File \ piechart.php超过300秒的最长执行时间

第249行是print_r(array_values($dataa));现在,这只是测试变量输出的一个测试。这是整个代码。

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

1 个答案:

答案 0 :(得分:2)

每次启动while循环时,您都会再次运行查询。因此,它运行无限次,导致脚本超时(如错误所示)。而不是:

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

这样做:

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

此外,您可能真的想要分离代码的各个部分。混合使用HTML,PHP和JavaScript是维护噩梦的一个方法,它使您的应用程序几乎不可能正常测试。