PHP中SQL数据表中未显示的所有值

PHP中SQL数据表中未显示的所有值,php,mysql,sql,Php,Mysql,Sql,我试图显示从MySQL到PHP的所有数据,如下所示,但只显示1个数据 <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 Ta

我试图显示从MySQL到PHP的所有数据,如下所示,但只显示1个数据

<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>

数据表
名称
位置
办公室
薪水

现在的问题是,表中只显示一个数据,如何使用相同的代码显示所有数据?有人能告诉我吗?

您的代码有几个问题,您似乎从未设置$name、$email和$company变量,并且您已经循环了一个数组,并在循环后设置了HTML,因此您将只显示一个结果

我马上就来

<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>
<?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'];

            <tr>
                <td><?=$row['id']; ?></td>
                <td><?=$row['name']; ?></td>
                <td><?=$row['email']; ?></td>
                <td><?=$row['company']; ?></td>
            </tr>

        }

        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);

}

?>
</table>

名称
位置
办公室
薪水
}
mysqli_免费_结果($result);
}否则{
echo“未找到与您的查询匹配的记录。”;
}
}否则{
echo“错误:无法执行$sql。”.mysqli_错误($link);
}
?>

您多次设置
$test
,但是你只能回显一次。@KIKOSoftware但是我怎么能在不回显每个值的情况下多次显示呢?你可以回显
while
循环中的表行,从数据库中检索数据。我看不到任何JavaScript。我能建议你去网上阅读几篇教程,开始新的学习吗处理查询结果的概念。如果您只将一件事放入数组中,那么可以从一个小的点开始,它会更快地执行
$test[]=$row
而不是
array\u push()
,因为您没有调用函数的开销
<?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);
     }
<?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 } ?>