简体   繁体   English

显示大量数据的最佳方法是什么(PHP)

[英]What is the best way to display a large set of data (PHP)

I have a function that connect to about a 1000+ databases and get the data back and put it in an array. 我有一个函数连接到大约1000多个数据库并获取数据并将其放入数组中。

however, the data is so large and I want to display the results on the browser each time the loop goes through a connection instead of waiting for the whole loop to finish. 但是,数据是如此之大,我希望每次循环通过连接时在浏览器上显示结果,而不是等待整个循环完成。

or if there is a better way of doing so, please share. 或者如果有更好的方法,请分享。

function Get_data()
{
 // loop base on the number of available databases
    for ($i = 0; $i < count($client_databases); $i++) {

        // Getting connection for each database
        $database = $this->connect_clients_db($client_databases[$i]);

       // return an array of data from the database
        $ClientShema = $this->getSomeData($database);

        // put the data in array
         $schemas[]=$ClientShema;
    }


     return $schemas;


}

example of results would be 结果的例子是

loop 1 (database_one) this is a data coming from database one loop 1(database_one)这是来自数据库1的数据

loop 2 (database_two) this is a data coming from database two loop 2(database_two)这是来自数据库2的数据

You can turn on output buffering and flush the buffer periodically to the browser. 您可以打开输出缓冲并定期刷新缓冲区到浏览器。

First, you have to send a certain amount of data to the browser: 首先,您必须向浏览器发送一定数量的数据:

echo str_repeat(" ", 40000);

Next, you need to start output buffering: 接下来,您需要启动输出缓冲:

ob_start();

Finally, after you've output what you want to send to the browser, you need to flush the output buffer. 最后,在输出要发送到浏览器的内容后,需要刷新输出缓冲区。 I found that I had to call the following three functions to get this to work: 我发现我必须调用以下三个函数才能使其工作:

ob_end_flush();
ob_flush();
flush();

So, your code might look like the following: 因此,您的代码可能如下所示:

function Get_data()
{
    echo str_repeat(" ", 40000);

    //loop base on the number of available databases
    for ($i = 0; $i < count($client_databases); $i++) {
        //Start buffering output
        ob_start();

        // Getting connection for each database
        $database = $this->connect_clients_db($client_databases[$i]);

        // return an array of data from the database
        $ClientShema = $this->getSomeData($database);

        // put the data in array
        $schemas[]=$ClientShema;

        //Write some output somewhere here.

        //flush the output buffer here.
        ob_end_flush();
        ob_flush();
        flush();
    }

    return $schemas;
}

Here is a block of code you can use to test this technique: 以下是可用于测试此技术的代码块:

<?php

echo str_repeat(" ", 40000);

for ($i = 0; $i < 10; $i++) {
    ob_start();

    echo "Hello world.<br/>";
    ob_end_flush();
    ob_flush();
    flush();

    usleep(500000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用PHP处理大数据的最佳方法是什么 - What is the best way to process large data in PHP 在PHP中定义大量数据的最佳方法是什么? - What is the best way to define a large array of data in PHP? 在Android上向PHP / MySQL后端发送和检索(可能是大量)数据的最佳方法是什么? - What's the best way to send and retrieve (possibly large amounts) data to a PHP/MySQL backend on Android? 处理大型数据集的图形和显示的最佳方法 - Best way to handle graphing and display of large data sets 用PHP创建大型xml文件的最佳方法是什么? - What is the best way to create a large xml file with PHP? 使用PHP读取多个大型JSON文件的最佳方法是什么 - what is the best way to read multiple large JSON files using PHP 在PHP中将大文件写入磁盘的最佳方法是什么? - What is the best way to write a large file to disk in PHP? PHP:获取 $_POST 数据的最佳方法是什么? - PHP: What is the best way to get $_POST data? 将大量数据迁移到另一台服务器的最佳方法是什么? - What's the best way to migrate large amounts of data to another server? 在PHP / MySQL中存储大量表单数据的最佳方法? - Best way to store large amounts of form data in PHP/MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM