Sami Aji Sami Aji - 2 months ago 7
PHP Question

Why is my PHP while loop repeating the same database result?

It is displaying six posts, and I have 6 items in my database. The issue is that it is only displaying the first one, and repeating it five more times. This is my first time writing PHP so please don't get too advanced with terminology!

<?php

$pagetitle = "My Portfolio | Projects";

$header = $_SERVER['DOCUMENT_ROOT'];
$header .= "/includes/header.php";
include_once($header);

$sql="SELECT * FROM projects ORDER BY job DESC";
$sql_query=mysql_query($sql);
$post = mysql_fetch_array($sql_query);
$job = $post['job'];
$category = $post['category'];
$title = $post['title'];
$inside = $post['imageinside'];
$outside = $post['imageoutside'];
$body = $post['body'];
$description = $post['description'];

?>


<!-- Blog - Start -->
<?php do {?>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 blog blog_altered blog_left">
<div class="row">
<!-- Blog Image - Start -->
<div class=" col-lg-6 col-md-6 col-sm-10 col-xs-12 pic inviewport animated delay1" data-effect="fadeIn">
<img alt="blog-image" class="img-responsive" src="<?php echo "$outside" ?>">
</div>
<!-- Blog Image - End -->
<!-- Blog Info - Start -->
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 inviewport animated delay1" data-effect="fadeIn">
<div class="info">
<span class="date"><?php echo "$category" ?></span>
<h4 class="title"><a href="<?php echo "$job" ?>.php"><?php echo "$title" ?></a></h4>
<p><?php echo "$description" ?></p>
<a class="btn btn-primary text-on-primary" href="<?php echo "$job" ?>.php">Read More</a>
</div>
</div>
<!-- Blog Info - End -->
</div>
</div>
<?php } while ($post = mysql_fetch_array($sql_query)) ?>
<!-- Blog - End -->

Answer

That is because you're already assigning it at the top of your script and using those variables later on. So the next time you pass the while it doesn't use the freshly fetched data, but simply the data from your previously set code.

Simply get rid of this part

$post = mysql_fetch_array($sql_query);
$job = $post['job'];   
$category = $post['category'];
$title = $post['title'];
$inside = $post['imageinside'];
$outside = $post['imageoutside'];
$body = $post['body'];
$description = $post['description'];

Change your do-while to a 'normal' while loop, and change your variables inside the html bit to the post ones. So <?php echo "$category" ?> with <?php echo "$post['category']" ?> etc.

With that said, you'll need this to get this part working. But it would be better to use a while loop, not a do, while. The difference between them being that a while loop will only execute when the data passed to the loop is actually holding something. Do-while will always execute ., and after that check for valid stuff inside your while. So when the database has no results the html block is still printed once.

You would also make a good choice to look into mysqli or pdo for database connections. mysql is no longer the preferred way (deprecated since 5.5 and removed in php 7)

Lastly, you could do with mysql_fetch_assoc if you're going to use the associative keys only.