8

We are upgrading PHP to version 8.1. Using MS Sql Server DB. It all seems to work correctly but I see repeated messages in the log file:

[03-Feb-2022 11:51:18 America/New_York] PHP Deprecated: Automatic conversion of false to array is deprecated in C:...\includes\adodb\drivers\adodb-mssqlnative.inc.php on line 154

I've updated adodb to version version 5.22 but that did not stop the messges from logging. The ini file has

extension=php_sqlsrv_81_nts_x64.dll
extension=php_pdo_sqlsrv_81_nts_x64.dll

Does anyone know how to fix this problem?

dregad
  • 1,150
  • 8
  • 21
kuru
  • 77
  • 1
  • 1
  • 4
  • 2
    Does the library support PHP/8.1? https://adodb.org/dokuwiki/doku.php?id=v5:php_compatibility_status – Álvaro González Feb 08 '22 at 14:14
  • I didn't see the issue mentioned in their list. It's a temporary fix so we might just go with ~DEPRICATED in the error logging. – kuru Feb 08 '22 at 14:46
  • I changed it from using mssqlnative to pdo and the problem went away. Thanks for the answers. – kuru Feb 08 '22 at 18:11
  • 1
    This issue has been [reported in the ADOdb issue tracker](https://github.com/ADOdb/ADOdb/issues/829) and will be fixed in ADOdb release 5.22.2 – dregad Apr 16 '22 at 08:35

1 Answers1

15

This is a known issue of backwards incompatibility, affecting the ADOdb library version 5.22.1 and older. PHP 8.1 warns you on autovivification of false-y values and some future version of PHP will throw an error when you do it.

PHP natively allows for autovivification (auto-creation of arrays from falsey values). This feature is very useful and used in a lot of PHP projects, especially if the variable is undefined. However, there is a little oddity that allows creating an array from a false and null value.

And they give this example

// From false
$arr = false;
$arr[] = 2;

I went and found the file in question and this is the function it's in

function ServerInfo() {
    global $ADODB_FETCH_MODE;
    static $arr = false;
    if (is_array($arr))
        return $arr;
    if ($this->fetchMode === false) {
        $savem = $ADODB_FETCH_MODE;
        $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
    } elseif ($this->fetchMode >=0 && $this->fetchMode <=2) {
        $savem = $this->fetchMode;
    } else
        $savem = $this->SetFetchMode(ADODB_FETCH_NUM);

    $arrServerInfo = sqlsrv_server_info($this->_connectionID);
    $ADODB_FETCH_MODE = $savem;
    $arr['description'] = $arrServerInfo['SQLServerName'].' connected to '.$arrServerInfo['CurrentDatabase'];
    $arr['version'] = $arrServerInfo['SQLServerVersion'];//ADOConnection::_findvers($arr['description']);
    return $arr;
}

The problem is that it starts with

static $arr = false;

and then tries to autovivificate a non-array (line 154 in your error)

$arr['description'] = $arrServerInfo['SQLServerName'].' connected to '.$arrServerInfo['CurrentDatabase'];

You should be able to fix it (in theory) by making sure it's an array (something they should have done anyways). Add this above that line to make it one before it tries to append

if(!is_array($arr)) $arr = [];
Machavity
  • 30,841
  • 27
  • 92
  • 100