简体   繁体   中英

execute mysql query and display results while reading

I have a mysql database and i want to execute a query and while this query is being executed the data should be displayed in page.

so for example if i have 1,000 result row from the query result i want to display each row while the query is being executed instead of waiting till the query finishes executing then displaying them at once.

here is my php code:

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
// Database Name
mysql_select_db("dbname", $con);
$test_query = mysql_query("SELECT * FROM V_Posts where _PID > 100 and _PID < 10000");
while($CHECK_PCID_R = mysql_fetch_array($test_query))
{
    echo $CHECK_PCID_R['_PID'] . "<br />";
}
?>

I tried

echo $CHECK_PCID_R['_PID'] . "<br />";
flush();

But it didn't work :(

One query will produce one dataset and you'll have all the data at once. If your query is slow any latency in displaying the data will be small compared to the delay in receiving it. Using flush() might force the server to send parts of the page, but you're really just tinkering at the edges.

If you want to break this down you'll have to run multiple queries, which will arguably be much slower since you'll be running the same query repeatedly. This will load the database server unnecessarily, and will achieve only a minor cosmetic effect.

If you use an AJAX call to retrieve your data you can display a 'loading' message while you wait. You could use multiple AJAX calls to display the data bit by bit - this is even worse than using multiple queries in the PHP script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM