Memory leak?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Memory leak?

Post by pinehead18 »

Code: Select all

<?php
<?php
db_connect('lkcforum'); 


        $sql = "SELECT name FROM forums WHERE forum_id='{$_GET['f']}'"; 
        $result = mysql_query($sql,$con) or die(mysql_error()); 
        $row = mysql_fetch_array($result); 
                $forum_name = $row['name']; 


       $sql = "SELECT subject,author,tid,forum_id FROM topics WHERE forum_id='{$_GET['f']}'"; 
       $result = mysql_query($sql,$con) or die(mysql_error()); 
       while ($row = mysql_fetch_array($result)) { 

                $subject = $row['subject']; 
                $topic_id = $row['tid']; 
                $author = $row['author']; 
                $forum_id = $row['forum_id']; 

        $sql = "SELECT date FROM threads WHERE tid='$topic_id'"; 
        $result = mysql_query($sql); 
        $row = mysql_fetch_array($result); 
                $date = $row['date']; 


        if (strlen($subject) > 42) { 
                        $filler = "..."; 
                        $subject = substr($subject, 0, 42 - $filler) . $filler; 
                } 

               $topics .= " 
                        <tr> 
                               <td width=60%><a href=viewtopic.php?tid=".$topic_id."&fid=$forum_id>$subject</a></td> 
                               <td width=20% align=center><a href=/myprofile/$author>$author</a></td> 
                               <td width=20%>$date</td> 
                           </tr>"; 


        }
?> 
?>
That is my entire code when i add the following code (which is above as well) i get an exhausted memory error

Code: Select all

<?php
 $sql = "SELECT date FROM threads WHERE tid='$topic_id'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
                $date = $row['date'];
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try freeing the results of that query after you've used it.. otherwise, it's probably staying resident in memory..
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

What do you mean by freeing the results?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your third query is being repeated over and over, this stores more and more results in memory until the page ends. You need to either alter your second query to fetch the date from that table, or you need to free the results of the third query after you've pulled the information and before the loop starts again.. [php_man]mysql_free_result[/php_man]()
Post Reply