Only the first row of the table being displayed!

1

I am requesting the table data this way:

    $sql = "SELECT * FROM 'login' order by 'userid' DESC";
$limite = mysqli_query($db, $sql);
while ($sql = mysqli_fetch_array($limite)) {
    $account_id = $sql['account_id'];
    $userid = $sql['userid'];
    $sex = $sql['sex'];
    $email = $sql['email'];
    $group_id = $sql['group_id'];
    $last_ip = $sql['last_ip'];
}
echo '
<table class="vertical-table th">
    <tr align="center">
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>ID</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Login</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Sexo</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Email</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Level</b></font></th>
      <th width="20%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>IP</b></font></th>
    </tr>
<tr align="center">
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#009900" size="2">'.$account_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$sex.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$email.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#E41B17" size="2">'.$group_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#000099" size="2">'.$last_ip.'</font></p></td>
    </tr>
    </table>

Testing the query directly in the database works perfectly, but on page only returns the first line ...

    
asked by anonymous 25.03.2015 / 13:06

3 answers

3

It is only displaying the values of the last record because its echo is after while , if you want to print all the records throw the code into the loop.

while ($sql = mysqli_fetch_array($limite)) {
    $account_id = $sql['account_id'];
}
echo '
//... mais código
<p style="margin-top: 0; margin-bottom: 0">
<font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>

You can correct when doing PHP interpolation with HTML:

<table class="vertical-table th">
    <tr align="center">
      <th width="10%">ID/th>
      <th width="10%">Login</b>/th>
    </tr>

<?php
    $sql = "SELECT * FROM 'login' order by 'userid' DESC";
    $limite = mysqli_query($db, $sql);
    while ($sql = mysqli_fetch_array($limite)) {
       $account_id = $sql['account_id'];
       $userid = $sql['userid'];
?>
       <tr align="center">
          <td><?php echo $account_id; ?></td>
          <td><?php echo $userid; ?></td>
       </tr> 
<?php 
    } 
?>
</table>
    
25.03.2015 / 13:11
3

To use all the data that comes from the Database you have to print them in all iterations of while , that is, as long as there are lines to come from the Database you print the lines of the table in the HTML.

echo '
<table class="vertical-table th">
    <tr align="center">
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>ID</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Login</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Sexo</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Email</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Level</b></font></th>
      <th width="20%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>IP</b></font></th>
    </tr>
';    

$sql = "SELECT * FROM 'login' order by 'userid' DESC";
$limite = mysqli_query($db, $sql);
while ($sql = mysqli_fetch_array($limite)) {
    $account_id = $sql['account_id'];
    $userid = $sql['userid'];
    $sex = $sql['sex'];
    $email = $sql['email'];
    $group_id = $sql['group_id'];
    $last_ip = $sql['last_ip'];

    echo ' 
    <tr align="center">
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#009900" size="2">'.$account_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$sex.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$email.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#E41B17" size="2">'.$group_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#000099" size="2">'.$last_ip.'</font></p></td>
    </tr>
   ';
}   
echo'</table>';
    
25.03.2015 / 13:20
2

while is the command that traverses the records of your query. You need it for the specific part of table you want to repeat, in your case it would look something like:

echo '
<table class="vertical-table th">
   <tr align="center">
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>ID</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Login</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Sexo</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Email</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Level</b></font></th>
      <th width="20%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>IP</b></font></th>
   </tr>
   ';

   while ($sql = mysqli_fetch_array($limite)) {
      $account_id = $sql['account_id'];
      $userid = $sql['userid'];
      $sex = $sql['sex'];
      $email = $sql['email'];
      $group_id = $sql['group_id'];
      $last_ip = $sql['last_ip'];

      echo '
      <tr align="center">
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#009900" size="2">'.$account_id.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$sex.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$email.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#E41B17" size="2">'.$group_id.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#000099" size="2">'.$last_ip.'</font></p></td>
      </tr>
      ';
   }
echo '</table>';
    
25.03.2015 / 13:26