显示<table> </table>中的所有表行

时间:2014-12-29 06:29:56

标签: php mysql

我创建了一个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>

正如您所看到的,现在我只尝试一行。到目前为止,我已经显示了最后一个注册表。我想向他们展示所有,我该怎么做?

4 个答案:

答案 0 :(得分:3)

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


echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($comments))
{
echo "<tr>";
echo "<td>" . $row['nome'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

做这样的事情

答案 1 :(得分:2)

试试这个:

    <?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>
      <tbody>
<?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);
?>
        <tr>
          <td><?php echo $name ?></td>
          <td>$email</td>
          <td>$website</td>
          <td>$comment</td>
          <td>$timestamp</td>
        </tr>
 <?php }

mysql_close($con); ?>


      </tbody>
    </table>

答案 2 :(得分:2)

试试这个你会在循环中犯错误

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

答案 3 :(得分:1)

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

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

      echo '<tr>';
      echo '<td>'.$name.'</td>'
              SAME ALL FIELDS
             ....
      echo   '</tr>';

}

mysql_close($con); ?>




      </tbody>
    </table>

尝试以上。