PHP在打印大量信息时很慢 [英] PHP is very slow when printing a large amount of information

查看:46
本文介绍了PHP在打印大量信息时很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我需要从数据库中检索大量行,然后将它们全部打印在屏幕上.我检查了 mysql 查询,这不是问题所在.问题是所有行都需要打印在同一页上,没有分页,而且需要很长时间(我说的是一个有几千行的表格).有什么办法可以加快速度吗?我在谷歌上发现的唯一的东西是使用,"而不是."使用回声时.我将对此进行测试以查看是否有任何改进,但我不确定它会产生如此大的差异.

I have an application where I need to retrieve a large amount of rows from the database and then print them all on the screen. I've checked the mysql queries, and that's not where the problem is. The problem is the rows all need to be printed on the same page, with no pagination, and it takes a long time (I'm talking about a table with several thoudands of rows). Is there any way to speed things up? The only thing I've found on Google is using "," instead of "." when using echo. I'll be testing this to see if there is any improvement, but I'm not sure it will make such a big difference.

推荐答案

问题不太可能是 PHP,而是大量的 HTML 输出到浏览器.您可以通过保存您获得的页面,然后从您的硬盘加载静态文件来自己验证这一点.

The problem is unlikely PHP, it's the massive amount of HTML being output to the browser. You could verify this yourself by saving the page that you get, then loading the static file from your hard drive.

如果您正在输出一个大表格,那么浏览器在加载内容时经常会遇到重绘"它的问题.这可以通过为所有列设置固定宽度来最小化.只需在第一行执行此操作.

If you are outputting a large table, then the browser will often have problems with "redrawing" it as content is loaded. This can be minimized by setting a fixed width for all the columns. Just do this on the first row.

你也可以跳出 PHP 块输出大部分 HTML,然后在适当的地方打印变量.这是一个示例,假设您的所有数据都在 $rows 变量中:

You can also jump out of the PHP block to output most of the HTML, then just print the variables where appropriate. Here's an example, assuming all your data is in the $rows variable:

<?php
  // some code here
?>

<table>
<thead>
<tr>
  <th width="50">Header 1</th>
  <th width="200">Header 2</th>
  <th width="150">Header 3</th>
  <th width="100">Header 4</th>
</tr>
</thead>
<tbody>
<?php foreach ( $rows as $r ) : ?>
<tr>
  <td><?=$r['var1']?><td>
  <td><?=$r['var2']?><td>
  <td><?=$r['var3']?><td>
  <td><?=$r['var4']?><td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<?php
  // continue here if necessary

注意:如果您没有启用 PHP 短标签,则需要执行 <?php echo $r['var1'] ?> 代替.

Note: if you don't have PHP short tags enabled you'll need to do <?php echo $r['var1'] ?> instead.

最后,您可以尝试添加 gzip 压缩.最好在服务器级别完成,但在 PHP 中,您可以添加行 ob_start("ob_gzhandler"); 作为 PHP 代码的第一行,紧跟 <?php.

Finally, you can try adding gzip compression. It's best done at a server level but in PHP you can do add the line ob_start("ob_gzhandler"); as the very first line of PHP code right after <?php.

如果这仍然没有帮助,那么很简单,您的桌子太大.您最好使用分页或过滤来减小尺寸.

If this still does not help, then it's a simple fact that your table is way too big. You'd be much better off using pagination or filtering to keep the size down.

这篇关于PHP在打印大量信息时很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆