PHP 8.1 Fix - PHP Warning: Trying to access array offset on value of type null - Error

Hi All

Forever I have been using the example of the below code to get results from my DB and add it to a veriable:

$getContent = mysqli_query($con,'SELECT whatever FROM table WHERE this="this" AND that="that"');		  
$getContent = mysqli_fetch_array($getContent);

I then use the code like - print $getContent['whatever'];

I have recently upgraded to PHP 8.1 and now get an error in my error log if the result is NULL:

PHP Warning: Trying to access array offset on value of type null in LINE NUMBER

Any idea how I fix this?

Thanks for any help


And you are sure this is the part of the code giving the error? I ask because nothing should have changed with this. PHP 8.1 didn’t change any behavior for that related function or it would have been documented in the mysqli_fetch_array documentation page. It would have given you the same error even back in PHP 7. Make sure that your $getContent is not being set to NULL. This would happen if your query doesn’t have anymore rows.

Could your query be different than it was before? Did maybe the row you originally select get deleted?


Thanks for your reply.

The error is happening all over the place on many pages on many websites.

In the error log the line that always has the error is the one with the “$getContent = mysqli_fetch_array($getContent);” code on it.

By the looks of it is is only happening if there is nothing in the database to return.

EXAMPLE: I have a system that does my books and looks for a tax code… if there is nothing in the DB for the tax code the result is NULL and then the error appears.

From my research I think I need to add something like if it is empty don’t do anything?

I have also had issues with my $_GET[‘somthething’]; code and have had to updates them all to something like - if (isset($_GET[‘somthething’]) == true) { $_GET[‘somthething’] = $_GET[‘somthething’]; } else { $_GET[‘somthething’] = “”; } which has fixed that error.

Any further ideas?


I’m still learning PHP, but I think that’s always been the case - if you try to use an array member that doesn’t exist, PHP has “always” to my knowledge complained about it.

Are you sure the log settings aren’t different?

That doesn’t make sense to me, because you’re not trying to access an array offset in that line of code - you’re trying to create an array by retrieving the next row from the results object. It’s the print row that actually tries to access an array element.

Aren’t you going to run into a problem anyway with this bit of code if it has more than one row returned because you’re overwriting the results object with the first row? Or does the example only ever return one row?

$getContent = mysqli_query($con,‘SELECT whatever FROM table WHERE this=“this” AND that=“that”’);
$getContent = mysqli_fetch_array($getContent);


That example always returnes 1 row only.

Another expample might be SELECT * FROM users WHERE userid=“1”;

That way I get all of that usedr information into a variable that I can use.

That example though would not return a NULL result.

The upgrade to my server to add PHP 8.1 may have changed what is logged and this may have always been an issue but not logged.

Any suggestion at all how to do this?

What do you do?


I worked it out… this is my final code:

$getContent = mysqli_query($con,‘SELECT something FROM whatever WHERE this=“this”’);

if (mysqli_num_rows($getContent) == 0) { } else { $getContent = mysqli_fetch_array($getContent); }

What would probably be better is checking if what you get back is NULL…

$getContent = mysqli_query($con,‘SELECT something FROM whatever WHERE this=“this”’);

if (is_null($getContent)) {
     // Throw some kind of exception

// Here would then be ok to use $getContent

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.