Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is wrong with my code I don't know what is wrong? but it says that Warning: Trying to access array offset on value of type null in C:\Users\Val\Downloads\XAMPP\htdocs\Login\firebase\station1.php on line 55-60


What I have tried:

PHP
  1  <?php  include('includes/header.php'); ?>
  2  <?php session_start(); ?>
  3  
  4  
  5   <div class="container">
  6   	<div class="row">
  7   		<div class="col-md-12">
  8   			<?php
  9                 if (isset($_SESSION['status']) && $_SESSION['status'] != "")
 10                 {
 11                 	?>
 12  				<div class="alert alert-warning alert-dismissible fade show" role="alert">
 13    					Hey <?php echo $_SESSION['status']; ?>
 14    					<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">	
 15    					</button>
 16  				</div>
 17                 	<?php
 18                      unset($_SESSION['status']);
 19                 } 
 20                  ?>
 21            </div>             			 			
 22  		<div class="col-md-12 mt-m5">
 23  			<div class="card">
 24  				<div class="card-body">
 25  					<div class="table-responsive">
 26  						<table class="table table-bordered">
 27  							<thead>
 28  								<tr>
 29  									<th>Title</th>
 30  									<th>Description</th>
 31  									<th>Location</th>
 32  									<th>Date_of_Incident</th>
 33  									<th>Time_of_Incident</th>
 34  									<th>Phone_No</th>
 35  									<th>Delete</th>
 36  
 37  									
 38  								</tr>
 39  							</thead>
 40  							<tbody>
 41  								<?php
 42  									include('includes/dbconfig.php');
 43  									$ref = "Station_1/";
 44  									$fetchdata = $database->getReference($ref)->getValue();
 45  									$i= 0;
 46  									if ($fetchdata > 0)
 47  									{
 48  
 49  
 50  									foreach ($fetchdata as $key => $row) 
 51  									{
 52  									 $i++;									
 53  								 ?>
 54  								<tr>
 55  									<td><?php echo $row['title']; ?></td>
 56  									<td><?php echo $row['description']; ?></td>
 57  									<td><?php echo $row['report_Location']; ?></td>
 58  									<td><?php echo $row['date_of_I  ncident']; ?></td>
 59  									<td><?php echo $row['time_of_Incident']; ?></td>
 60  									<td><?php echo $row['phone_Number']; ?></td>									
 61  									<td>
 62  										<form action="code.php" method="POST">
 63  											<input type="hidden" name="ref_toke_delete" value="<?php echo $key; ?>">
 64  											<button type="submit" name="delete_data" class="btn btn-danger">Delete</button>
 65  										</form>
 66  									</td>
 67  									
 68  								</tr>
 69  								<?php 
 70  									}
 71  									}									
 72  								?>
 73  								<a href="logout.php">Logout</a>	
 74  							</tbody>
 75  						</table>
 76  					</div>
 77  				</div>
 78  			</div>
 79  		</div>
 80  
 81  
 82  	</div>
 83  </div>
Posted
Updated 25-Feb-21 23:34pm
v2

Start by looking at what lines are involved: 55-60 are whet it says, but we have no idea which those are.

Most text editors sue CTRL+G to go directly to a specific line, so I'd start by doing that.

When you know which line(s) are involved, look for indexers: stuff in square brackets.
What the error is saying is that you are applying an indexer to a NULL value: x[a] with x being NULL.
We have no idea what x is, or what it should contain - so it's going to be up to you and the debugger to work that out before you can fix it!
 
Share this answer
 
Quote:
Trying to access array offset on value of type null in C:\Users\Val\Downloads\XAMPP\htdocs\Login\firebase\station1.php on line 55-60

I am not php expert, but I know what means the warning: the $row variable is not what you expect.
There is an easy way to find out, use the debugger and inspect the variables at warning point.
I suspect
PHP
foreach ($fetchdata as $key => $row)

is wrong.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
The statement:
PHP
foreach ($fetchdata as $key => $row)

is not returning a value in the $row variable, so all the following references are invalid. What do you expect to be returned in $fetchdata?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900