play_ Posted July 20, 2007 Share Posted July 20, 2007 Ok. The title wasn't very descriptive nor correct. here's the problem. I have a blog, with comments. Here's the full code: <?php include ('./includes/header.php'); // news query | display the news // $sql = new mysql(); $sql->query("SELECT news_id, title, author, news, DATE_FORMAT(date_submitted, '%b %D, %y @ %l:%i %p') FROM news ORDER BY date_submitted DESC LIMIT 3"); while($row = $sql->fetchObject() ) { echo '<div class="entry_holder">'; echo '<div class="title">'. $row->title .'</div>'; echo '<div id="news">'. nl2br($row->news) .'</div>'; echo '</div>'; } include ('./includes/footer.php'); ?> Alright. The output of that can be seen here: http://www.decay.nu/index2.php What i'm trying to do is post the comments under each post. So then the whole code becomes this: <?php include ('./includes/header.php'); // news query | display the news // $sql = new mysql(); $sql->query("SELECT news_id, title, author, news, DATE_FORMAT(date_submitted, '%b %D, %y @ %l:%i %p') FROM news ORDER BY date_submitted DESC LIMIT 3"); while($row = $sql->fetchObject() ) { echo '<div class="entry_holder">'; echo '<div class="title">'. $row->title .'</div>'; echo '<div id="news">'. nl2br($row->news) .'</div>'; echo '</div>'; // comments query | display comments // $sql->query("SELECT commentID, name, website, comment, date_submitted, news_id FROM comments where news_id = '$news_id' order by commentID DESC"); } include ('./includes/footer.php'); ?> The output(problem) of that can be seen here: http://decay.nu/index3.php Notice how in the second block of code, there's another query. the comments query seems to replace the news/blog query, therefore breaking the stop query. the sql class can be seen here: http://decay.nu/class.phps And sorry about the bold. It's so you guys can differentiate code from regular text easier. Quote Link to comment Share on other sites More sharing options...
phpknight Posted July 20, 2007 Share Posted July 20, 2007 Have you tried uses two separate instances of the class (like $sql1 and then $sql2)? That might fix it. Then, you might have to check if it is already connected when you call connect. Quote Link to comment Share on other sites More sharing options...
play_ Posted July 20, 2007 Author Share Posted July 20, 2007 Thank you. Will try soon. Quote Link to comment Share on other sites More sharing options...
play_ Posted July 20, 2007 Author Share Posted July 20, 2007 It worked. Thanks a bunch New question: Because the loop goes around 3 times, a new object gets created each time.(i think) Is there a way to destroy an object? And also: Personal Message (Online) How to fix this: $query keeps getting replaced. « on: Today at 12:44:42 AM » Reply with quoteQuote Ok. The title wasn't very descriptive nor correct. here's the problem. I have a blog, with comments. Here's the full code: <?php include ('./includes/header.php'); // news query | display the news // $sql = new mysql(); $sql->query("SELECT news_id, title, author, news, DATE_FORMAT(date_submitted, '%b %D, %y @ %l:%i %p') FROM news ORDER BY date_submitted DESC LIMIT 3"); while($row = $sql->fetchObject() ) { echo '<div class="entry_holder">'; echo '<div class="title">'. $row->title .'</div>'; echo '<div id="news">'. nl2br($row->news) .'</div>'; echo '</div>'; } Notice how, to echo the title, i do $row->title. But how would i echo the DATE_FORMAT? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 20, 2007 Share Posted July 20, 2007 I think you would do $row->date_submitted. Not 100% sure though. If not, try changing your query to: $sql->query("SELECT news_id, title, author, news, DATE_FORMAT(date_submitted, '%b %D, %y @ %l:%i %p') as date_submitted FROM news ORDER BY date_submitted DESC LIMIT 3"); Quote Link to comment Share on other sites More sharing options...
phpknight Posted July 20, 2007 Share Posted July 20, 2007 I'd have to see the new code to see if the object gets created three times. You can create the object before the loop starts. That way it uses the same one. Otherwise, PHP will automatically take care of that with garbage collection when the object is out of scope or at the end of the script. So, I wouldn't worry about it unless it starts giving you issues. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.