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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why mysqli_fetch_assoc Returns Null and How to Fix Your Loop

If you are staring at an error message like Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on line 41, the culprit is almost certainly a logic flaw in your database retrieval loop. In this specific case, the issue isn't with the database connection or the SQL query itself; it's with how you are iterating over the result set and attempting to access variables that haven't been assigned yet.

Looking at the code provided, the error occurs inside the <tbody> of the table where you attempt to render a row. You initialize $i = 0, enter the while loop, fetch an association into $rows, increment $i, but then immediately fail to assign the column data to their respective variables before trying to echo them.

Here is the problematic block from your code:

<?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>
    <!-- ... subsequent cells accessing $rows directly ... -->
</tr>

The logic here is broken in two distinct ways. First, the while loop executes its body (fetching data and incrementing the counter) but contains no statements to actually store the fetched data from $rows into variables like $name, $dob, or $sex. However, even if you added those assignments inside the loop, the fatal flaw would remain: the code attempts to render a <tr> tag outside of the while loop.

In PHP, a variable is only populated while it is in scope within its specific execution block. When the while loop finishes (either because all rows are fetched or the query returned zero results), the variable $rows effectively goes out of scope for the next iteration of code. The HTML block <tr>...</tr> is positioned immediately after the closing ?> tag of the PHP block. At this point in execution, the script has finished processing the loop. Consequently, when the page tries to echo $rows['student_id'], PHP looks for that variable and finds nothing—it is technically null or undefined depending on the context—triggering the "access array offset on value of type null" warning.

To fix this, you must ensure that every row is rendered inside the loop where the data exists. Move the <tr> tag inside the while statement so that it only attempts to print data when $rows actually contains a valid record. Additionally, you need to assign the values from the fetched array to your variables before echoing them.

Here is the corrected implementation:

<tbody>
    <?php 
        $i = 0;
        while($rows = mysqli_fetch_assoc($result)){
            $i++;
            // Assign data from the fetched row to specific variables
            $student_id = $rows['student_id'];
            $name = $rows['Name'];
            $dob = $rows['DOB'];
            $sex = $rows['sex'];
            $class = $rows['Class'];
        ?>
        <tr>
            <th scope="row"><?php echo $i; ?></th>
            <td><?php echo $student_id; ?></td>
            <td><?php echo $name; ?></td>                        
            <td><?php echo $dob; ?></td>                        
            <td><?php echo $sex; ?></td>                        
            <td><?php echo $class; ?></td>
        </tr>
        <?php 
        } 
        ?>
    </tbody>

There is also a subtle bug in your SQL query logic: SELECT * FROM tbl_student ORDER BY id DESC. If you are fetching all rows to display them, ordering by ID descending will show the newest students first. While valid, ensure this aligns with your UI expectations. If you simply want to list everyone without a specific sort order, omit the ORDER BY clause or choose an ID that makes sense for pagination if you implement it later.

Finally, always handle the case where the query returns no results. Your current code has a check if (mysqli_num_rows($result)), which is good practice to prevent rendering an empty table header unnecessarily, though modern Bootstrap handles empty tables gracefully enough. However, ensuring your PHP variables are populated only when data exists is the primary defense against "null" warnings in array access operations.

Our network