projectnami icon indicating copy to clipboard operation
projectnami copied to clipboard

PHP 7.4 Notice warning for accessing a Array Offset when Array is null

Open A-Matt opened this issue 4 years ago • 3 comments

Accessing a Page Edit screen returns the following between the Name & the URL, however this issue likely can be caused in more places where the get_var function does not return a row. Notice: Trying to access array offset on value of type null in <dir>\html\wp-includes\wp-db.php on line 2612 (Please note the line maybe slightly off due to a minor edit within our config)

	if(false === $result)
		return null;

	$row = sqlsrv_fetch_array( $result );
	return $row[ 0 ];   // <---- This line
}

"SELECT TOP 1 post_name FROM wp_posts WHERE post_name = '<POSTNAME>' AND post_type IN ( 'page', 'attachment' ) AND ID != 5227 AND post_parent = 4882" is the Failing SQL Query in my case. This is likely to no rows returned and can be fixed by changing it to:

Fix

return sqlsrv_has_rows( $result ) ? $row[ 0 ] : null;

or however else you'd prefer.

Currently at work so can make a basic PR Request later fixing this issue.

Edit: Information

SQLSRV: 5.9.0+14116 PHP: 7.4.15 (NTS) Nami: 5.7.2 (recent update from 5.3.2)

A-Matt avatar Jun 11 '21 11:06 A-Matt

I've been unable to duplicate this on other PN sites running on PHP 7.4. Any thoughts on that?

patrickebates avatar Jul 07 '21 15:07 patrickebates

I can reproduce this on SQL Server 15.0.4138.2 / PHP 8.0.9 / Nami 2.8.0 / WP 5.8.

This is on a fresh install of Wordpress, with fresh database and only one post.

@A-Matt's fix works for me.

xadammr avatar Aug 11 '21 12:08 xadammr

Since introducing this fix we've not had an issues in our System. The fix is likely due to some where in the code it's not correctly setup and is returning a null value somewhere where it shouldn't. The issue can also happen within Plugins ETC where they maybe looking for a specific thing and will get this error.

The query looks to be done at this point here: https://github.com/ProjectNami/projectnami/blob/4701ad96ac2f5d62155ee7ac6e93fd29ea436532/wp-includes/post.php#L4810-L4815

A-Matt avatar Aug 11 '21 13:08 A-Matt