从mysql加载1000个结果,等待时间为5分钟

时间:2015-03-20 05:56:48

标签: php mysql

我希望在单个页面上执行以下代码,但是有超过1千个结果需要几分钟才能显示有没有办法让它加载页面然后从表中加载结果说一次10个没有我必须点击任何东西

$results = mysqli_query($con,"SELECT * FROM email_list");
echo "<table>";
while($row2 = mysqli_fetch_array($results))
{
echo"<tr>";
echo "<td>" .  $row2['email_idno'] . "</td>";
echo "<td>" .  $row2['email_email'] . "</td>";
echo "<td>" .  $row2['email_name'] . "</td>";   
echo"</tr>";            
}
echo "</table>";    
经过大量研究后,我认为这是可能的,但无法解决如何做到这一点,任何帮助都是非常苛刻的

2 个答案:

答案 0 :(得分:0)

您可以在此处执行三项操作,以加快页面加载速度。

  1. 最简单的解决方案是查询优化和数据库索引。谷歌可以更快地从数据库访问数据。它可能并不总能带来更快的结果。但它更容易学习和实施。

  2. 分页。加载100个结果限制。你不需要在这里学习任何新语言。

  3. 的Ajax。这是最适合您需求的解决方案。但是你需要学习ajax。还学习如何通过jquery使用ajax。如果对于简单的时间限制任务,这是耗时的。 程序很简单..你加载你的页面。然后你编写的javascript函数(imho使用jquery)加载并联系服务器php页面,返回结果。然后,结果再次通过javascript显示在页面上。现在,当你一次做10个时,复杂性会增加。

  4. 学习ajax和jquery(转到任何教程网站)

    如果您决定使用ajax

    ,这些链接会对您有所帮助

    http://www.w3schools.com/php/php_ajax_database.asp

    https://phpseason.wordpress.com/2013/02/15/ajax-add-retrieve-mysql-records-using-jquery-php/

    Ajax on interval

    How to fire AJAX request Periodically?

    Timely interval to retrieve data from db, and stop when the data arrives

答案 1 :(得分:0)

以下是一个完整的例子:

呼叫者:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>title</title>
        <link rel="stylesheet" href="style.css">
        <script src="jquery.js"></script>
    </head>
    <script>
    $(document).ready(function()
    {
        iOffset = 0;
        function callEndpoint( call_url, payload ){
            return $.ajax({
                url: call_url,
                type: 'GET',
                data: payload
            });
        }
        function loadRows() {
            iLimit  = 10;
            oRequest = callEndpoint( '/play/endpoint2.php', { 'offset': iOffset, 'limit': iLimit } );
            oRequest.done(function( sJson ) {
                aRaw = JSON.parse( sJson );
                        console.log( aRaw );
                aData = aRaw.data;
                if( jQuery.isEmptyObject( aData ) )
                {
                    console.log( 'empty!! ' );
                    clearInterval( t );
                }
                else
                {
                    sNewRows = '';
                    for ( var i = 0; i < aData.length; ++i ) {
                        sNewRows += '<tr>';
                        for ( var prop in aData[ i ] ) {
                            if( aData.hasOwnProperty( prop ) ){
                                sNewRows += '<td>' + aData[ i ][ prop ] + '</td>';
                            }
                        }
                        sNewRows += '</tr>';
                    }

                    $( '#loading-data tr:last' ).after( sNewRows );
                    iOffset += 10;
                }
            });
        }
        loadRows();
        var t = setInterval(function(){
            loadRows();
        },2000); // Load more rows every 2 seconds.
    });
    </script>
    <body>
        <table id="loading-data">
            <tbody>
                <thead>
                    <tr>
                        <th>PK</th>
                        <th>co</th>
                        <th>type</th>
                        <th>num</th>
                    </tr>
                </thead>
            </tbody>
        </table>
    </body>
</html>

端点:

class MySql
{
    private $sDbName      = 'play';
    private $sUsername    = 'root';
    private $sPassword    = '';
    private $sHost        = 'localhost';
    private $oConnection  = null;

    public function __construct()
    {
        $this->oConnection = new PDO( 
            'mysql:host=' 
            . $this->sHost 
            . ';dbname=' 
            . $this->sDbName, 
            $this->sUsername, 
            $this->sPassword 
            );
    }
    public function getDb()
    {
        return $this->oConnection;
    }
}
$oMySql = new MySql;
$oDb = $oMySql->getDb();

$aGet = $_GET;
if( !empty( $aGet ) )
{
    $iOffset = $aGet[ 'offset' ];
    $iLimit  = $iOffset + $aGet[ 'limit' ];
}

$sSql = "
    SELECT pk, companyId, type, page_nav
    FROM
    `1`
    WHERE
    pk >= :custom_offset
    AND 
    pk < :custom_limit
    ";
$oStmp = $oDb->prepare( $sSql );

$oStmp->bindValue( ':custom_offset', $iOffset );
$oStmp->bindValue( ':custom_limit', $iLimit );
$oStmp->execute();
$aResults = $oStmp->fetchAll();
// var_dump( $aResults );
$oErrors = $oStmp->errorInfo();
// var_dump( $oErrors );

$aReturn[ 'data' ] = $aResults;
$sJson = json_encode( $aReturn, 1 );
header( 'Content-type', 'application/json' );
echo $sJson;