1

I'm following a tutorial on how to create a dynamic blog system when I ran into this error, Parse error: syntax error, unexpected 'endwhile' (T_ENDWHILE), expecting end of file.

<?php
//connect to DB
include('includes/db_connect.php');
$query = $db->prepare("SELECT post_id, title, body FROM posts");
$query->execute();
$query->bind_results($post_id, $title, $body);
?>

<!DOCTYPE html>
<html lang="en">

  <body>

    <!-- Page Content -->
    <div class="container">

      <!-- Page Heading/Breadcrumbs -->
      <br><br>
      <h1 class="mt-4 mb-3" >News</h1>
      <hr>
      <div class="row">
        <!-- Blog Entries Column -->
        <div class="offset-lg-2">
        <!-- Paste New Entries Here -->

          <!-- Blog Post -->
          <div class="card mb-3">
            <div class="card-body">
              <?php while($query->fetch()); ?>
              <h2 class="card-title"><?php echo $title ?></h2>
              <p class="card-text"><?php echo $body ?></p>
              <a class="btn btn-CMD" href="#">Read More &rarr;</a>
              <?php endwhile; ?>
            </div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
VisioN
  • 143,310
  • 32
  • 282
  • 281
  • for me it was old script of mine (WP them) I was using `` php open brackets it's should be ` – Salem Dec 02 '21 at 21:50

2 Answers2

2

There is a typo in your code. while statement, that has endwhile, should end with colon:

<?php while($query->fetch()): ?>

Alternatively, you may wrap your code in curly brackets:

<?php while($query->fetch()) { ?>
    <h2 class="card-title"><?php echo $title ?></h2>
    <p class="card-text"><?php echo $body ?></p>
    <a class="btn btn-CMD" href="#">Read More &rarr;</a>
<?php } ?>

MORE: http://php.net/manual/en/control-structures.while.php

VisioN
  • 143,310
  • 32
  • 282
  • 281
2

There should be : after while, not ;:

<?php while($query->fetch()): ?>
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177