Create a table in HTML with php consulting a database. What is the best option? [closed]

0
<?php


require "conexion.php";

$conexion = new mysqli($host,$user,$pass,$baseDatos);

if($conexion->connect_errno)
{
    echo "Error de conexion de la base datos".$conexion->connect_error;
    exit();
}
$sql = "select * from medicos";

$resultado = $conexion->query($sql);
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1 align="center">LISTADO DE MEDICOS</h1>
    <table width="70%" border="1px" align="center">

    <tr align="center">
        <td>Codigo</td>
        <td>Identificacion</td>
        <td>Nombre</td>
        <td>Apellidos</td>
        <td>Especialidad</td>
        <td>Telefono</td>
        <td>Correo</td>
    </tr>
    <?php 
        while($datos=$resultado->fetch_array()){
        ?>
            <tr>
                <td><?php echo $datos["idMedico"]?></td>
                <td><?php echo $datos["medIdentificacion"]?></td>
                <td><?php echo $datos["medNombres"]?></td>
                <td><?php echo $datos["medApellidos"]?></td>
                <td><?php echo $datos["medEspecialidad"]?></td>
                <td><?php echo $datos["medTelefono"]?></td>
                <td><?php echo $datos["medCorreo"]?></td>
            </tr>
            <?php   
        }

     ?>
    </table>

</body>
</html>
    
asked by antonio asensio soria 23.02.2017 в 14:42
source

2 answers

2

your code is fine but to order do the following: require "conexion.php"; You must know all the connection parameters including the error so the following code should append them to the file

$conexion = new mysqli($host,$user,$pass,$baseDatos);

if($conexion->connect_errno)
{
    echo "Error de conexion de la base datos".$conexion->connect_error;
    exit();
}

also how do I mention To Cedano for questions of querys optimization you must add the fields to the selection

select idMedico,medIdentificacion,medNombres,medApellidos,medEspecialidad,medTelefono,medCorreo from medicos

With respect to the table I would do it this way, as I explained in the comment

    <tr>
        <td><?=$datos["idMedico"]?></td>
        <td><?=$datos["medIdentificacion"]?></td>
        <td><?=$datos["medNombres"]?></td>
        <td><?=$datos["medApellidos"]?></td>
        <td><?=$datos["medEspecialidad"]?></td>
        <td><?=$datos["medTelefono"]?></td>
        <td><?=$datos["medCorreo"]?></td>
    </tr>
    
answered by 23.02.2017 в 16:28
1

The option you chose is valid, another way of doing it that is clearer to read without so much opening and closing of php tags would be

    while($datos=$resultado->fetch_array()){
    echo'
        <tr>
            <td>'.$datos["idMedico"].'</td>
            <td>'.$datos["medIdentificacion"].'</td>
            <td>'.$datos["medNombres"].'</td>
            <td>'.$datos["medApellidos"].'</td>
            <td>'.$datos["medEspecialidad"].'</td>
            <td>'.$datos["medTelefono"].'</td>
            <td>'.$datos["medCorreo"].'</td>
        </tr>';

    }

 ?>
    
answered by 23.02.2017 в 15:51