What I want is to display all the records of a table (SQL)
in PHP
by means of a table.
This is the table:
<div class="col-md-8">
<table class="table">
<thead class="table-success table-striped">
<tr>
<th>Id</th>
<th>Nombre</th>
<th>Apellido</th>
<th>Edad</th>
<th>Fecha de nacimiento</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query)) {
?>
<tr>
<th><?php echo $row['id'] ?></th>
<th><?php echo $row['nombre'] ?></th>
<th><?php echo $row['apellido'] ?></th>
<th><?php echo $row['edad'] ?></th>
<th><?php echo $row['fecha_de_nacimiento'] ?></th>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
This is the code PHP
with which I call the registers
<?php
include("funcionalidad/conexion.php");
$conn = conectar();
$sql = "SELECT * FROM usuario";
$query = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($query);
?>
the strange thing is that it works, but not as I expected since I have 2 records in the database
The correct and modern way would be to store everything first and then expose the html.
Separate as much as you can the html from the logic in php it helps to give it better maintenance and if your logic fails your php code will break and not your html, when you get to use templates you will appreciate it.
As I mentioned, you are not generating the dynamic html.
You are always sending the last record, so no new data is inserted.
What you should change is this part of the
tbody
, to the following:I would recommend doing it with foreach friend, with while you only send a data.