显示多个数据库查询结果,一次一个,每个数据库之间有一些延迟

时间:2018-03-13 22:17:53

标签: php sql-server

我是一名初学者PHP编码器,所以如果我的代码看起来很疯狂,请事先道歉。

我的任务是(尝试)编写一个程序,它将循环遍历数据库列表(以及服务器,但我会稍后再说明这部分),向每个数据库发送相同的查询,然后显示几个结果秒,然后是下一个查询...我接近这一点,我可以按照我想要的方式显示所有结果,但它在同一页面上是一个接一个的,你需要用户交互向下滚动到查看每个db结果。我试图以一种方式执行此操作,只需一次循环数据库并在没有用户交互的情况下显示它(它将在电视上供用户全天参考)。另一个问题是我坚持使用5.3更新24我认为(我们有其他程序使用它并且不会运行任何更新的东西(那是另一天的另一个项目)。

我尝试在循环结束时使用session_start()session_write_close()session_destroy()Sesson_reset(),但这只是重新初始化相同的结果,而不是示例使用database1的结果绘制表,等待3秒,清除屏幕,然后在循环中显示下一个数据库结果。

我已经接近放弃了,因为我找不到任何其他人这样做过,或者至少没有人问过我能找到的同一个问题。我的代码如下,查询是匿名的..

    <html>
        <head>
            <title>stuff older than 3 days</title>
            <link rel="stylesheet" type="text/css" href="style.php"/>
        </head>
        <body>

<?php

/*
* Comments here
*/
$serverName = "192.168.0.90";
$databaseName = "";
$dbUsername = "xxdbuserxx";
$dbUserpass = "xxdbpassxx";
$db_Name_Array = parse_ini_file("dbname.ini");
$db_Srv_Array = parse_ini_file("srvname.ini");
$k=0;
$countq=0;

//Loop through each database listed in the ini file
foreach($db_Name_Array as $section=>$values){
        //$databaseName=$section


            $connectionInfo = array("UID" => $dbUsername, "PWD" => $dbUserpass, "Database" => $section);        
            $link = sqlsrv_connect($serverName, $connectionInfo);
            //read the array and print
            $sql = "SELECT Field1, Field2, Field3, Field4, Field5, Field6 from Table;

            //stmt1 is solely for retrieving the number of rows, unfortunately it cant be retrieved from the normal stmt query.

            $stmt1 = sqlsrv_query( $link, $sql, array(), array('Scrollable' => 'buffered'));

            //display the number of results in the case that there are too many to see on the screen
            $countq=sqlsrv_num_rows($stmt1);
            // now do the same query but for displaying them to the screen
            $stmt = sqlsrv_query( $link, $sql);
            echo "<h2>$values  ----- ( # of sessions older then 3 days: $countq )</h2>";
            echo"<table class='table1'>";
            //set the Header

        echo"<tr class='qtop1'><td>field1</td><td>field2</td><td>Field3</td><td>Field4</td><td>Field5</td><td>Field6</td></tr>\n";

        //loop through the results changing color of every other row to make it easier to read

        while ($row = sqlsrv_fetch_array($stmt)){
            if ( isset($k) and $k==0){  
            echo"<tr class='d0'><td>{$row['Field1']}</td><td>{$row['Field2']}</td><td>{$row['Field3']}</td><td>{$row['Field4']}</td><td>{$row['Field5']}</td><td>{$row['Field6']}</td></tr>\n";
            $k=1;
            }
            else{
            echo"<tr class='d1'><td>{$row['Field1']}</td><td>{$row['Field2']}</td><td>{$row['Field3']}</td><td>{$row['Field4']}</td><td>{$row['Field5']}</td><td>{$row['Field6']}</td></tr>\n";
            $k=0;       
            }

        }
        echo"</table>";
        if( $stmt === false ){  
        die( print_r( sqlsrv_errors(), true));
        }
}

?>

        </body>
    </html>

1 个答案:

答案 0 :(得分:0)

将以下代码放在网页的<head>部分。

<meta http-equiv="refresh" content="3">

这将告诉浏览器每3秒刷新/重新加载一次。

将session_start()保留在页面上,但删除所有其他session _ *()函数。

<强> 修改

你想要的是自动刷新的分页。查看代码,您需要进行以下更改:

<?php
session_start();
//--- keep track of where we are
if( !isset($_SESSION['PageCount']) )
   $_SESSION['PageCount'] = 0;
/*
* Comments here
*/
$serverName = "192.168.0.90";
$databaseName = "";
$dbUsername = "xxdbuserxx";
$dbUserpass = "xxdbpassxx";
$db_Name_Array = parse_ini_file("dbname.ini");
$db_Srv_Array = parse_ini_file("srvname.ini");
$k=0;
$countq=0;

$itemsPerPage = 2; //--- how many entries to display Must be more than 1
$currentPage = $_SESSION['PageCount'];

//--- loop through number of databases that need to be displayed
for($x=0; $x<$itemsPerPage; $x++ ) {
   //--- get the info about the db and server from the respective arrays
   $dbName = $db_Name_Array[$currentPage];
   $dbSrv  = $db_Srv_Array[$currentPage];
   //--- make the connection to the database using $dbName and $dbSrv info

   //--- get the data and format it

   //--- increment the current page number
   $currentPage++;
   //--- set to zero to loop to the first entry
   if( $currentPage >= count( $db_Name_Array) ) {
        $currentPage = 0;
   }
}
//--- save the current page number to the session var
$_SESSION['PageCount'] = $currentPage;
?>

以上将列出2个或更多条目($ itemsPerPage)。

$ _SESSION ['PageCount']会跟踪要显示的项目。

如代码所示,列表中的最后一项将是下次刷新时列表中的第一项。