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:xampphtdocscrudread.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/<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7b5b8b8a3a4a3a5b6a797e2f9e6f9e6">[email protected]</a>/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>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 }
} ?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0