Home > front end >  Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on
Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\crud\read.php on

Time:10-01

I want to create a website that can do the basic my SQL functions like create, read, update and delete and I was working on the read page when I got this error:

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

Here is my code :

<?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/[email protected]/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>

CodePudding user response:

Your table rows (from <tr> to </tr>) should be inside the while loop. The $rows variable is not defined outside of it.

                        <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 }
                    } ?>
  • Related