Jump to content

How to fix this: $query keeps getting replaced.


play_

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.