Mateus Guimarães
Mateus Guimarães

Reputation: 176

Show all table rows in <table>

I made a html table and I am able to retrieve data from the mysql table. However, I'm able to show the last registry only. I'd like to show them all.

My code:

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

As you can see, for now I'm trying one row only. So far, I'm getting the last registry shown. I want to show them all, how can I do it?

Upvotes: 4

Views: 178

Answers (4)

Niko Jojo
Niko Jojo

Reputation: 1226

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

Try Above.

Upvotes: 1

Vamshi .goli
Vamshi .goli

Reputation: 520

Try with this you will get you have made a mistake in while loop

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

Upvotes: 2

Anju
Anju

Reputation: 502

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

Do something like this

Upvotes: 3

amit
amit

Reputation: 874

try this:

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

Upvotes: 2

Related Questions