0

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)) ...

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

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.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
expream
  • 21
  • 1
  • 2
    I think he understands the mistake he made (he described it as an accident in the first line). His question was why the server never killed the script because it was in a loop. Perhaps this question would be better asked in a server administration site, e.g. serverfault.com. – Barmar Feb 03 '14 at 01:01
  • Well... the OP already has access to the resource of the query, so should iterate over. Looks like someone isn't trying hard enough – planestepper Feb 03 '14 at 01:20

1 Answers1

1

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(...)).

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

Edit: 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.

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!

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Maybe my English is bad, but I will repeat myself. I do know why code is not working, I know how to solve it. I ask about php limits. Why php limit does not work in this case. – expream Feb 03 '14 at 01:30
  • I misunderstood your question at first; I have edited my answer to address why PHP ignores the memory limit. – elixenide Feb 03 '14 at 02:09
  • So the problem is "not assigning result to variable"? Is it php bug? Looks like it is. So in this situation user is able to avoid php limits and take down server. – expream Feb 03 '14 at 10:52
  • Less a PHP bug per se, more of a mod_php bug / old MySQL library bug. The user isn't able to avoid the PHP limits unless you have a bug in your code; if your code didn't get into an infinite loop, it would use far less memory and wouldn't spiral out of control. Of course, a malicious user can *always* overwhelm a server by a DDoS attack (sending too much traffic at the server from lots of addresses), but that has nothing to do with PHP; that's true, no matter what technologies you use. – elixenide Feb 03 '14 at 14:14
  • Do not compare DDoS attack and simple loop :) I have updated PHP and MySQL to latest versions, have used mysqli lib and got same result. Memory limit does not work in that scenario. – expream Feb 06 '14 at 23:02
  • I don't know what to tell you, other than don't create infinite loops. :) Clearly, Apache is allocating additional memory on each query, but mod_php isn't detecting that the limit is exceeded. – elixenide Feb 07 '14 at 00:34