PHP:我没有选择我在表中插入的所有数据,但有些人会帮助我

发布于 2025-01-17 12:20:15 字数 1328 浏览 0 评论 0原文

我很好地在表中插入了数据,它显示在 phpMyadmin 中,但是当我尝试显示数据库中的数据时,只显示一项。我需要帮助。

下面的代码和屏幕截图 下面的

<?php 
                
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
    $count = mysqli_num_rows($result);
        if($count > 0){
            while($row = mysqli_fetch_assoc($result)){
               $incidentId = $row['id'];
               $icTypeName = $row['inctype_name'];
               $addedBy = $row['added_by'];
               $addedOn = $row['added_on'];
            }

?>
            <tr class="tableData">
              <td><?php echo $incidentId;?></td>
              <td><?php echo $icTypeName;?></td>
              <td><?php echo $addedBy;?></td>
              <td><?php echo $addedOn;?></td>
              <td><i class="uil uil-edit icon editIcon"></i></td>
            </tr>

<?php
           } else {
             $_SESSION['noData'] = 'No incidents to show!';
           }
        } else {
          die('Failed to Connect!');
        }       
?>

图片

在此处输入图像描述 在此处输入图像描述

我希望获取使用“SELECT * FROM nw”显示的数据库表中的所有数据

I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.

Code and screenshoots below

<?php 
                
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
    $count = mysqli_num_rows($result);
        if($count > 0){
            while($row = mysqli_fetch_assoc($result)){
               $incidentId = $row['id'];
               $icTypeName = $row['inctype_name'];
               $addedBy = $row['added_by'];
               $addedOn = $row['added_on'];
            }

?>
            <tr class="tableData">
              <td><?php echo $incidentId;?></td>
              <td><?php echo $icTypeName;?></td>
              <td><?php echo $addedBy;?></td>
              <td><?php echo $addedOn;?></td>
              <td><i class="uil uil-edit icon editIcon"></i></td>
            </tr>

<?php
           } else {
             $_SESSION['noData'] = 'No incidents to show!';
           }
        } else {
          die('Failed to Connect!');
        }       
?>

Images below

enter image description here
enter image description here

I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月松间行 2025-01-24 12:20:15

只需将结束的while循环结束的}移动到使用变量的代码之后。您当前在输出任何内容之前都要消耗所有结果集,因此您只会看到最后的行数据。

<?php 
                
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
    $count = mysqli_num_rows($result);
        if($count > 0){
            while($row = mysqli_fetch_assoc($result)){
               $incidentId = $row['id'];
               $icTypeName = $row['inctype_name'];
               $addedBy = $row['added_by'];
               $addedOn = $row['added_on'];
            //}   REMOVED

?>
            <tr class="tableData">
              <td><?php echo $incidentId;?></td>
              <td><?php echo $icTypeName;?></td>
              <td><?php echo $addedBy;?></td>
              <td><?php echo $addedOn;?></td>
              <td><i class="uil uil-edit icon editIcon"></i></td>
            </tr>

<?php
            } // END OF WHILE LOOP
        } else {
            $_SESSION['noData'] = 'No incidents to show!';
        }
    } else {
        die('Failed to Connect!');
    }       
?>

Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.

<?php 
                
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
    $count = mysqli_num_rows($result);
        if($count > 0){
            while($row = mysqli_fetch_assoc($result)){
               $incidentId = $row['id'];
               $icTypeName = $row['inctype_name'];
               $addedBy = $row['added_by'];
               $addedOn = $row['added_on'];
            //}   REMOVED

?>
            <tr class="tableData">
              <td><?php echo $incidentId;?></td>
              <td><?php echo $icTypeName;?></td>
              <td><?php echo $addedBy;?></td>
              <td><?php echo $addedOn;?></td>
              <td><i class="uil uil-edit icon editIcon"></i></td>
            </tr>

<?php
            } // END OF WHILE LOOP
        } else {
            $_SESSION['noData'] = 'No incidents to show!';
        }
    } else {
        die('Failed to Connect!');
    }       
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文