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.

2
  • 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. Commented Feb 3, 2014 at 1: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 Commented Feb 3, 2014 at 1:20

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

6 Comments

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.
I misunderstood your question at first; I have edited my answer to address why PHP ignores the memory limit.
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.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.