PHP Deprecated: Automatic conversion of false to array is deprecated in adodb-mssqlnative.inc.php
Upgrading from PHP 7.x to PHP 8.1 often introduces a wave of deprecation notices that can clutter your logs and obscure genuine errors. A particularly persistent offender when working with SQL Server is the warning: Automatic conversion of false to array is deprecated. This specific error originates within the ADOdb library, specifically in the adodb-mssqlnative.inc.php driver file.
The root cause lies in a fundamental shift in PHP's type system behavior. In previous versions, if a variable was treated as an array when it actually held a boolean false, PHP would automatically cast that false to an empty array. This silent coercion allowed code like $data = [] !== false ? $data : get_data() to function without immediate failure, assuming the fallback value would be used. PHP 8.1 explicitly forbids this automatic casting to maintain type safety and prevent subtle bugs where data structures are unexpectedly modified.
When you see this warning in adodb-mssqlnative.inc.php, it indicates that a specific line of code within the ADOdb library is checking for an empty result set or an error condition, expecting an array back, but receiving false instead. The library then attempts to use that false value as if it were an array to iterate over rows or access keys.
Updating ADOdb to version 5.22 often feels like a silver bullet, but in this specific scenario involving the MSSQL Native driver (php_sqlsrv), the fix is not always found in the library version alone. The issue frequently stems from how the underlying php_sqlsrv extension interacts with the database connection state and how ADOdb interprets the return value of the native driver's execution functions.
Investigating the Driver Interaction
The core problem usually manifests when executing a query that returns no rows. The native SQL Server driver might signal "no rows found" by returning false, whereas older drivers or PDO modes often returned an empty array. ADOdb's internal logic, particularly in lines around 154 of the MSSQL Native driver, likely assumes that if a query is executed successfully (no exception thrown), the result handle will behave like an array-like object or that the fetch mechanism handles the false return gracefully by converting it.
In PHP 8.1, this assumption breaks. The code inside ADOdb tries to access keys on the result set immediately or passes the false value into a function expecting an array, triggering the deprecation notice.
To resolve this without waiting for another upstream release of ADOdb, you must ensure that the data being passed between the native driver and ADOdb's internal processing is explicitly cast to an array before the deprecation check occurs. However, since adodb-mssqlnative.inc.php is part of the library, modifying it directly is a temporary workaround unless you are comfortable maintaining a patched fork.
The Practical Fix: Explicit Casting
If you are on PHP 8.1 and cannot wait for ADOdb to patch this specific driver interaction, the most reliable approach is to modify the adodb-mssqlnative.inc.php file in your application directory (or the vendor directory if not autoloading from composer) at line 154.
You need to locate the logic where the result is retrieved or processed. The pattern often looks like this:
// Original problematic logic (simplified representation)
if (!$res) {
$res = []; // Or similar implicit conversion happening downstream
}
$row = db_fetch($res);
In PHP 8.1, you must ensure that the variable holding the database result is explicitly converted to an array before it reaches the point where ADOdb treats it as a collection. If the native driver returns false on zero rows, the code handling this return value must explicitly convert it:
$res = $driver->Execute($sql);
// Ensure res is always an array-like structure or null, never false
if ($res === false) {
$res = [];
} else {
// Handle the actual result set object if using standard ADOdb abstraction
// or ensure the fetch method handles the empty state correctly
}
However, since the error points to adodb-mssqlnative.inc.php, the fix is likely inside that specific file. Look for where the function checks for an empty result and forces a conversion. You might need to change a line that implicitly casts or checks !$res to ensure an array is returned consistently.
Alternative: Switching Drivers
If modifying ADOdb feels too risky for your production environment, consider switching from the php_sqlsrv native driver back to PDO with SQLSRV (php_pdo_sqlsrv). While you have both extensions loaded in your php.ini, ADOdb's native driver offers a different abstraction layer than PDO.
PDO tends to return empty arrays consistently even when no rows are found, which aligns better with PHP 8.1's expectations. You can configure your connection string to use the standard SQLSRV driver via PDO if your application stack allows it. This often eliminates the need for ADOdb's custom MSSQL Native adapter entirely, removing the deprecated code path from your execution flow.
Conclusion
The deprecation warning is a signal that your legacy abstraction layer (ADOdb) is colliding with modern PHP type safety rules. While updating libraries is standard practice, sometimes the underlying driver implementation requires explicit handling of edge cases like empty result sets. By ensuring that false return values from the native driver are explicitly cast to arrays before being processed by ADOdb's internal logic, you can silence these warnings and maintain compatibility with PHP 8.1 without sacrificing functionality.