如何解决“意外的'endwhile'”错误?

时间:2019-01-31 14:41:25

标签: php html

当我遇到此错误(解析错误)时,我正在跟踪有关如何创建动态博客系统的教程:语法错误,意外的“ endwhile”(T_ENDWHILE),期望文件结尾。

<?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>

2 个答案:

答案 0 :(得分:2)

您的代码中有错字。具有while的{​​{1}}语句应以冒号结尾:

endwhile

或者,您也可以将代码放在大括号中

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

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

答案 1 :(得分:2)

:之后应该有while,而不是;

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