Echo 500 records from database one by one

Discussion in 'PHP' started by huanghh, Dec 11, 2010.

  1. #1
    I'm trying to use the following script to output a list of directories. I'm expecting the script to print out the directories one by one in browser. However, it will just waiting for the server for long time, and then out put all the records in one time.

    What's wrong with the script? I need to watch on the browser and the script prints out the directories one by one instead of waiting for long time and print out in one time.

    BTW, I tried out bufferring without success.

    
    <html>
    ...
    <body>
    <?php
    ...
    //Around 500 records in $rs
    $rs = mysql_query("SELECT id name url pr remarks FROM free_dirs");
    while(list($id, $name, $url, $pr, $remarks) = mysql_fetch_array($rs)){
    //do something here takes around 15 seconds
    echo "<table width=\"800\">";
    if($id%2==0) echo "<tr bgcolor=\"#fef8ed\">";
    else echo "<tr bgcolor=\"#d7ef9c\">";
    echo "<td>$id</td>";
    echo "<td><a href=\"$url\" target=\"_blank\"> $name</a></td>";
    echo "<td>$pr</td>";
    echo "<td>$remarks</td>";
    
    }
    ...
    ?>
    </body>
    </html>
    
    PHP:
     
    huanghh, Dec 11, 2010 IP
  2. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Could it be a simple html issue that your browser is having trouble rendering the page?

    I see you have no close tr or close table being output. Also, your open table is inside the loop. So you are opening 500 tables, closing none and closing no rows.

    Try just outputting a bulleted list at first and see if it displays quickly, then work on your html issues.
     
    fr33lanc3, Dec 11, 2010 IP
  3. ankit_frenz

    ankit_frenz Active Member

    Messages:
    1,111
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    63
    #3
    You cannot fully control it..it depends on lot of things..php output buffering..turn it off from php ini..unfortunately even if you do that..many browsers do a buffering of there own..so you cant be 100% sure it will print line by line..still use ob clean and ob flush..along with fflush
     
    ankit_frenz, Dec 11, 2010 IP
  4. huanghh

    huanghh Active Member

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Do have close tags in my script, just forgot here.

    I tried ob_clean, ob_flush, no success.
     
    huanghh, Dec 11, 2010 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Uhm... HTML is not asynchronous - you put in a command (say a PHP-script) and it renders the page when the sript is done. Simple as that. If you want to see each line show up one by one as they're fetched from the database, you need to add an AJAX-query, or reload the page for each line (each record) you process. Elsewise it'll output the complete list when it's done, simple as that.
     
    PoPSiCLe, Dec 15, 2010 IP
  6. animebuzz.tv

    animebuzz.tv Peon

    Messages:
    317
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you need to generate one static page one time and use that static page to display.

    1.) run your script to save the whole html code (with the listings) to static.html
    2.) mainpage.php will include(static.html);
    3.) viola no need to stress the server and wait how long it'll take to finish the loading process
     
    animebuzz.tv, Dec 15, 2010 IP