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!