警告:尝试访问第 41 行 C:\xampp\htdocs\crud\read.php 中 null 类型值的数组偏移量

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41

我想创建一个可以执行我的 SQL 基本功能(如创建、读取、更新和删除)的网站,当我收到此错误时正在读取页面上工作:

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41.

这是我的代码:

<?php
    $link = mysqli_connect("localhost", "root", "", "db_school");
    if (mysqli_connect_error()) {
        die ("Database Connection Error");
    }
    $query = "SELECT * FROM tbl_student ORDER BY id DESC";
    $result = mysqli_query($link, $query);
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Read</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
    </head>
    <body>
            <?php
                include 'procces_read.php';
            ?>
        <div class="container">
            <div class="box">
                <h4 class="display-4 text-center">Read</h4>
                <br>
                <?php if (isset($_GET['success'])) { ?>
                    <div class="alert alert-success" role="alert">
                        <?php echo $_GET['success']; ?>
                    </div>
                <?php } ?>
                <?php if (mysqli_num_rows($result)) { ?>
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th scope="col">#</th>
                                <th scope="col">Student ID</th>
                                <th scope="col">Student Name</th>
                                <th scope="col">Student DOB</th>
                                <th scope="col">Student Sex</th>
                                <th scope="col">Student Class</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                                $i = 0;
                                while($rows = mysqli_fetch_assoc($result)){
                                    $i++;
                                }
                            ?>
                            <tr>
                                <th scope="row"><?php echo $i; ?></th>
                                <td><?php echo $rows['student_id'];?></td>
                                <td><?php echo $rows['Name']; ?></td>                       
                                <td><?php echo $rows['DOB']; ?></td>                        
                                <td><?php echo $rows['sex']; ?></td>                        
                                <td><?php echo $rows['Class']; ?></td>
                            </tr>
                        <?php } ?>
                    </tbody>
                </table>
                <div class="link-right">
                    <a href="create.php" class="link-primary">Create</a>
                </div>
            </div>
        </div>
    </body>
</html>

您的 table 行(从 <tr></tr>)应该在 while 循环内。 $rows 变量未在其外部定义。

                        <tbody>
                            <?php 
                                $i = 0;
                                while($rows = mysqli_fetch_assoc($result)){
                                    $i++;
                            ?>
                            <tr>
                                <th scope="row"><?php echo $i; ?></th>
                                <td><?php echo $rows['student_id'];?></td>
                                <td><?php echo $rows['Name']; ?></td>                       
                                <td><?php echo $rows['DOB']; ?></td>                        
                                <td><?php echo $rows['sex']; ?></td>                        
                                <td><?php echo $rows['Class']; ?></td>
                            </tr>
                        <?php }
                    } ?>