List MySQL Data in PHP

0

I have a list of names, emails and messages that I want to show, my problem is that when you show it, only the first of the records is displayed and it is repeated many times.

I attach the code:

<?php
//Conectar con el servidor
$link=mysqli_connect('localhost','USER','PASS');
if(!$link){
    echo'No se pudo establecer conexion con el servidor:'.mysql_error();
}else{
    //Seleccionamos Base de datos
    $base=mysqli_select_db($link, 'DATABASE');
    if(!$base){
        echo'No se encontro la base de datos:'.mysqli_error();
    }else{
    //Sentencia SQL
    $sql= "SELECT * FROM datos";
    $ejecuta_sentencia = mysqli_query($link, $sql);
    if(!ejecuta_sentencia){
        echo'Hay un error en la sentencia SQL:' .mysqli_error();
    }else{
    //Traer los resultados como array para poder imprimir
        $lista_usuarios = mysqli_fetch_array($ejecuta_sentencia);
        if(!lista_usuarios){
            echo'Error al mostrar lista de usuarios:' .mysqli_error();
        }
    }

    }

}
?>
<!DOCTYPE html>

<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=gb18030">
        <title>Listado de Usuarios</title>
        <link rel="stylesheet" type="text/css" href="estilo.css">
    </head>
    <body>
        <h1>Mostrando Usuarios desde Base de Datos</h1>
        <table>
            <tr>
                <th>Nombres</th>
                <th>Correo Electronico</th>
                <th>Mensaje</th> <?php
                        while($row=mysqli_fetch_array($ejecuta_sentencia))
                         for($i=1; $i<$lista_usuarios; $i++)
                        {   
                        echo"<tr>";
                            echo"<td>".$row['nombres']."</td>";
                            echo"<td>".$row['email']."</td>";
                            echo"<td>".$row['message']."</td>";
                            echo"<td></td>";
                        echo"</tr>";
                        }

                    ?>

            <tr>
        </table>
    </body>
</html>

I am starting in the world of PHP, I would appreciate your enormous help.

    
asked by Rafael Garcia 29.05.2018 в 23:13
source

2 answers

1

The problem is that you do not need the for loop inside the while . The while already iterates over all the records. You are also reading the first record before starting the loop. The correct way would be like this:

<?php
//Conectar con el servidor
$link=mysqli_connect('localhost','USER','PASS');
if(!$link){
    echo'No se pudo establecer conexion con el servidor:'.mysql_error();
}else{
    //Seleccionamos Base de datos
    $base=mysqli_select_db($link, 'DATABASE');
    if(!$base){
        echo'No se encontro la base de datos:'.mysqli_error();
    }else{
      //Sentencia SQL
      $sql= "SELECT * FROM datos";
      $ejecuta_sentencia = mysqli_query($link, $sql);
      if(!$ejecuta_sentencia){
          echo'Hay un error en la sentencia SQL:' .mysqli_error();
      }else{
          echo'Error al mostrar lista de usuarios:' .mysqli_error();
       }
    }

    }

}
?>
<!DOCTYPE html>

<html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=gb18030">
        <title>Listado de Usuarios</title>
        <link rel="stylesheet" type="text/css" href="estilo.css">
    </head>
    <body>
        <h1>Mostrando Usuarios desde Base de Datos</h1>
        <table>
            <tr>
                <th>Nombres</th>
                <th>Correo Electronico</th>
                <th>Mensaje</th> <?php
                        while($row=mysqli_fetch_array($ejecuta_sentencia)) {                              
                          echo"<tr>";
                            echo"<td>".$row['nombres']."</td>";
                            echo"<td>".$row['email']."</td>";
                            echo"<td>".$row['message']."</td>";
                            echo"<td></td>";
                          echo"</tr>";
                        }

                    ?>

            <tr>
        </table>
    </body>
</html>
    
answered by 29.05.2018 в 23:27
0

The problem you have is that you are making a loop and do not cut it, so it will be repeated as many times as you have records.
Close it before closing the last one with endwhile     

answered by 30.05.2018 в 13:22