LISTADO SIMPLE DE REGISTROS A UNA TABLA
El programa abre la base por debajo de la cabecera de tabla (donde le damos los estilos a cada columna) y
luego si se devuelven registros (mysqli_num_rows) un while (fetch_array devuelve
true si al avanzar un registro no llega al end of file) carga cada linea de la tabla con los valores correspondientes.
No olvidar el free_results y close finales y cerrar la etiqueta .
En la sentencia SELECT LIMIT 1,15 nos da los registros del 1 al 15.
<head>
<?
include("conexion.php"); // abre la conexión a la base de datos
global $conn; //lo usamos en conexion, lo declaramos acá para no llamar siempre a $GLOBALS[]
?>
<style>
table{
border-spacing: 0px;
border:none;
}
td{
border-bottom:#333 solid 1px;
margin: 0px;
padding: 5px;
padding-bottom:3px;
padding-top:3px;
}
</style>
</head>
<body>
<table>
<tr>
<td width="40" style=" text-align:center">ID</td>
<td width="200" style=" text-align:center">NOMBRE</td>
<td width="200" style=" text-align:center">DIRECCION</td>
<td width="200" style=" text-align:center">MAIL</td>
<td width="160" style=" text-align:center">NACIMIENTO</td>
</tr>
<?
if(conectar()){
$sqltxt = "SELECT * FROM clientes LIMIT 1,15";
$sqldevolucion = mysqli_query($conn,$sqltxt);
if (!mysqli_num_rows($sqldevolucion)) {
echo("NO HAY REGISTROS");
//aqui va un bloque de código de error, no se
//devuelven registros con ese SQL Select
} else {
while ($registro = mysqli_fetch_array($sqldevolucion, MYSQLI_ASSOC)){
echo("<tr>");
echo("<td>" . $registro["id"] . "</td>" );
echo("<td>".$registro["nombre"] . "</td>" );
echo("<td>".$registro["direccion"] . "</td>" );
echo("<td>".$registro["mail"] . "</td>" );
echo("<td style=' text-align:center;'>".$registro["fecha_nac"] . "</td>" );
echo("</tr>");
}
mysqli_free_result($sqldevolucion); //devuelvo recurso a memoria
mysqli_close($conn); // cierro conexion
}
}else{
echo(mysqli_error($conn)); //no se puede conectar con la DB
}
?>
</table>
</body>