Skip to content
Advertisement

all values not displaying from SQL data table in PHP

I am trying to display all my data from MySQL to PHP like the following, but only 1 data is being displayed

<div class="content">
<div class="animated fadeIn">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<strong class="card-title">Data Table</strong>
  </div>
<div class="card-body">

<?php

// Attempt select query execution
$sql = "SELECT * FROM registers";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            $test= $row['id'];
        }
        // Free result set
        mysqli_free_result($result);
    } else{
        echo "No records matching your query were found.";
    }
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
?>
<table id="bootstrap-data-table" class="table table-striped table-bordered">
  <thead>
  <tr>
    <th>Name</th>
  <th>Position</th>
  <th>Office</th>
    <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
    <td><?php echo $test; ?></td>
  <td><?php echo $name?></td>
  <td><?php echo $email?></td>
  <td><?php echo $company?></td>
  </tr>

  </tbody>
  </table>
  </div>
    </div>
    </div>

now the problem is, only one data is being displayed in the table, how can I display all data with my same code? can anyone please tell?

Advertisement

Answer

<?php

  // Attempt select query execution
   $sql = "SELECT * FROM registers";
  $test=array();
  if($result = mysqli_query($link, $sql)){
         if(mysqli_num_rows($result) > 0){
                while($row = mysqli_fetch_array($result)){

                      array_push($test,$row);
                }
           // Free result set
             mysqli_free_result($result);
            } else{
               echo "No records matching your query were found.";
        }
     } else{
         echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
     }

In your view part, you have add foreach to print all values:

<?php 
foreach($test as $record){?>
 <tr>
    <td><?php echo $test['id']; ?></td>
  <td><?php echo $test['name'];?></td>
  <td><?php echo $test['email'];?></td>
  <td><?php echo $test['company']?></td>
  </tr>
<?php } ?>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement