本文介绍了在PHP MySQL查询循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此脚本,Apache使用所有可用内存和服务器无响应:

When I run this script, apache uses all available memory and the server becomes unresponsive:

$db = new mysql();
$result = $db->query($sql);
while($row = $db->query($sql)) ...

为什么PHP限制不限制Apache进程? PHP正在按 mod_php的与Apache ITK。

Why does PHP limit does not limit apache process? PHP is working as mod_php with apache itk.

我有PHP极限的php.ini 设置,我看到在此限制的phpinfo()。我没有设置PHP脚本的任何限制。

I have PHP limit set in php.ini and I see this limit in phpinfo(). I do not have set any limit in PHP script.

推荐答案

正如你已经知道,在而($行= $ DB->查询($的SQL))导致一个无限循环;它只是反复运行查询,直到服务器死亡或杀死的过程。你需要做的而($行= $ result->取())而($行= $ result-> FETCH_ASSOC( ...))

As you already know, the while($row = $db->query($sql)) causes an infinite loop; it just runs the query over and over until the server dies or kills the process. You need to do while($row = $result->fetch()) or while($row = $result->fetch_assoc(...)).

另外,请<一href=\"http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1\">don't使用的mysql _ * ;在的mysql _ * 的功能是过时的,的和不安全。使用或来代替。

Also, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead.

编辑:我起先不明白你的问题。这里有一个问题:在mod_php,并且运行Apache,这意味着它通过Apache进程请求的内存。因此,通过 $ DB-GT及所需的内存;查询()被Apache,不是一个单独的mod_php的过程中使用。没有与mysql_ *函数一些已知的记忆问题。例如,如所描述here, 的mysql_query()所有缓存从查询数据。 mod_php的是,很显然,没有跟踪它,但是,因为你从来没有真正赋予这些数据PHP变量。所以,Apache的内存占用量不断增加,但mod_php,并且没有意识到这一点,所以在php.ini内存限制从不踢。

I didn't understand your question at first. Here's the problem: mod_php runs within Apache, meaning it requests memory through the Apache process. So, the memory required by $db->query() is used by Apache, not a separate mod_php process. There are some known memory problems with the mysql_* functions. For example, as described here, mysql_query() buffers all of the data from the query. mod_php is, apparently, not tracking it, however, because you are never actually assigning that data to PHP variables. So, Apache's memory footprint keeps growing, but mod_php doesn't realize it, so the php.ini memory limit never kicks in.

正如我前面所说,你应该避免mysql_ *函数和MySQL类,并使用库MySQLi或PDO。这种错误行为是一个很好的理由。祝你好运!

As I said above, you should avoid the mysql_* functions and MySQL class, and use either MySQLi or PDO. This kind of buggy behavior is one good reason for that. Good luck!

这篇关于在PHP MySQL查询循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 09:35