Show all table rows in table <table>
I created an html table and I can get data from mysql table. However, I can only show the latest registry. I would like to show them everything.
My code is:
<?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, at the moment I am trying only one line. So far I am showing the latest registry. I want to show them to everyone, how can I do this?
+4
4 answers
Try with this, you will get an error during the 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>
+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);
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
+3
try the following:
<?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
<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 the above.
+1