Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show all table rows in <table>

Tags:

php

mysql

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?

like image 804
Mateus Guimarães Avatar asked Feb 12 '23 06:02

Mateus Guimarães


1 Answers

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

like image 120
Anju Avatar answered Feb 13 '23 20:02

Anju