php – 显示所有表格行

我制作了一个html表,我可以从 mysql表中检索数据.但是,我只能显示最后一个注册表.我想向他们展示一切.

我的代码:

<?php

$con = mysql_connect("localhost","x","x");

if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("x", $con);


$query = "SELECT * FROM noticias";

$comments = mysql_query($query);


while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
  $name = $row['nome'];
  $email = $row['email'];
  $website = $row['lugar'];
  $comment = $row['comment'];
  $timestamp = $row['data'];

  $name = htmlspecialchars($row['nome'],ENT_QUOTES);
  $email = htmlspecialchars($row['email'],ENT_QUOTES);
  $website = htmlspecialchars($row['lugar'],ENT_QUOTES);
  $comment = htmlspecialchars($row['comment'],ENT_QUOTES);

}

mysql_close($con); ?>




    <table class="heavyTable">
      <thead>
        <tr>
          <th>Nome</th>
          <th>E-mail</th>
          <th>Lugar</th>
          <th>Notícia</th>
          <th>Data</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><?php echo $name ?></td>
          <td>$email</td>
          <td>$website</td>
          <td>$comment</td>
          <td>$timestamp</td>
        </tr>
      </tbody>
    </table>

如你所见,现在我只尝试一行.到目前为止,我正在显示最后一个注册表.我想向他们展示所有,我该怎么做?

试试这个你会让你在while循环中犯了一个错误

<?php

$con = mysql_connect("localhost","x","x");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("x", $con);


$query = "SELECT * FROM noticias";

$comments = mysql_query($query);
?>
 <table class="heavyTable">
  <thead>
    <tr>
      <th>Nome</th>
      <th>E-mail</th>
      <th>Lugar</th>
      <th>Notícia</th>
      <th>Data</th>
    </tr>
  </thead>

<?php

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{
$name = $row['nome'];
$email = $row['email'];
$website = $row['lugar'];
$comment = $row['comment'];
$timestamp = $row['data'];

$name = htmlspecialchars($row['nome'],ENT_QUOTES);
$email = htmlspecialchars($row['email'],ENT_QUOTES);
$website = htmlspecialchars($row['lugar'],ENT_QUOTES);
$comment = htmlspecialchars($row['comment'],ENT_QUOTES);



mysql_close($con); ?>




      <tbody>
    <tr>
      <td><?php echo $name ?></td>
      <td>$email</td>
      <td>$website</td>
      <td>$comment</td>
      <td>$timestamp</td>
    </tr>
 <?php }?>
  </tbody>
 </table>
相关文章
相关标签/搜索