0

我有一个包含 12000 种产品的数据库。我有一个查询会在一个页面上显示大约 3000 个产品,并且需要一段时间才能显示。然而,数据会在整个查询或循环完成后显示。无论如何,在它们被拉出时显示每个结果吗?以下是我的代码..

$result = mysql_query("SELECT title, model, description FROM products WHERE price > 500");
while($row = mysql_fetch_array($result)):
...
endwhile;

任何帮助表示赞赏。谢谢

4

3 回答 3

2

It's called lazy loading from db.

Take a look a this article or to this tutorial

If I have to make a synthesis, basically you LIMIT your query to a certain quantity of record that have to be pulled out of db. Once some condition is reached (pagination change, scroll down, and so on) you fire an ajax call that will retrieve other records and so on.

于 2013-03-27T14:22:25.740 回答
1

mysql 扩展已被弃用,最好切换到 mysqli 或 pdo。但由于不管 api/module 是什么,这都是同一个问题,所以我们现在还是坚持使用 mysql_* ......
mysql_query(...)被调用时,该函数仅在完整的结果集从 MySQL 服务器传输到 php 进程的内存后才返回。
改用mysql_unbuffered_query(...)

<?php
$result = mysql_unbuffered_query("SELECT title, model, description FROM products WHERE price > 500");
while($row = mysql_fetch_array($result)):
...
  // output
  // maybe flush() or ob_flush() here...
endwhile;

有区别解释在

您还必须牢记潜在的输出缓冲区:http: //docs.php.net/flushhttp://docs.php.net/ob_flush

对于 html 客户端/浏览器,您必须将部分数据放入可以在所有数据发送之前呈现的结构中。因此,例如,一个不固定的表很可能是次优的。参见例如https://en.wikipedia.org/wiki/Incremental_rendering

于 2013-03-27T14:25:02.630 回答
0

要达到这样的效果有很多变数。

首先,php 设置应该允许您使用输出缓冲(看看 output_buffering 参数,也许还有implicit_flush)。之后,您可能应该使用 flush() 和/或 ob_flush()

您应该考虑的另一件事是浏览器本身并不总是允许您使用输出缓冲,这是已知的“问题”。

对于查询本身,您可能想尝试使用 mysql_unbuffered_query(或更好的 MySQLi/PDO 等效项)。

综上所述,在我看来,达到这种效果的最好方法是使用 ajax 数据轮询。

于 2013-03-27T14:28:28.153 回答