【问题标题】:Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41 [closed]警告:尝试访问第 41 行 C:\xampp\htdocs\crud\read.php 中 null 类型值的数组偏移量 [关闭]
【发布时间】:2021-11-23 03:41:09
【问题描述】:

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

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

这是我的代码:

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

【问题讨论】:

  • 那是因为你做错了while循环。你在那里什么都不做,只是增加 $i 计数器变量。输出下面的表格行,属于 into 循环,而不是在它之后。
  • 你的 while 循环没有做任何事情。毕竟移动右大括号echo

标签: php mysql arrays database crud


【解决方案1】:

您的表格行(从&lt;tr&gt;&lt;/tr&gt;)应该在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 }
                    } ?>

【讨论】:

  • 非常感谢 Saphir 成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 2021-07-23
  • 2020-12-11
相关资源
最近更新 更多