ProjectNami / projectnami

WordPress powered by Microsoft SQL Server
http://projectnami.org
Other
270 stars 137 forks source link

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

Open A-Matt opened 3 years ago

A-Matt commented 3 years ago

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)

patrickebates commented 3 years ago

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

xadammr commented 3 years ago

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.

A-Matt commented 3 years ago

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