简体   繁体   English

执行mysql查询并在读取时显示结果

[英]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. 我有一个mysql数据库,我想执行一个查询,执行此查询时,数据应显示在页面中。

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. 因此,例如,如果我有1000条查询结果行,我想在执行查询时显示每一行,而不是等到查询完成后再显示它们。

here is my php code: 这是我的PHP代码:

<?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. 使用flush()可能会强制服务器发送页面的一部分,但实际上您只是在修改边缘。

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. 如果您使用AJAX调用来检索数据,则可以在等待时显示“正在加载”消息。 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. 您可以使用多个AJAX调用来一点一点地显示数据-这比在PHP脚本中使用多个查询还要糟糕。

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

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