Laravel 10 stopped working with SQL Server Database – Laravel

by
Ali Hasan
composer-php laravel sql-server

Quick Fix: Downgrading PHP version or temporarily commenting out PDO::ATTR_STRINGIFY_FETCHES in sqlsrv section of config/database.php and in $options of vendor/laravel/framework/src/Illuminate/Database/Connectors/SqlServerConnector.php can resolve the issue until a fixed sqlsrv extension is released.

The Problem:

A Laravel 10 application has suddenly stopped connecting to a SQL Server database, resulting in an error message ‘SQLSTATE[IMSSP]: An invalid attribute was designated on the PDO object.’ The issue persists despite attempts to resolve it. The application can establish a connection to MySQL databases, but not to SQL Server. Further investigation reveals that the operating system of the server is AlmaLinux 8.8. The issue seemed unresolvable using available resources, prompting a request for assistance to identify the root cause and potential solutions.

The Solutions:

Solution 1: Downgrade PHP version or temporarily disable PDO attribute

Problem: Laravel 10 suddenly stopped connecting to a SQL Server database, causing an "An invalid attribute was designated on the PDO object" error.

Reason: This error is due to a bug in the sqlsrv extension in PHP versions 8.1.22 and 8.2.9. A fix for MySQL uncovered a bug in sqlsrv, specifically related to an unsupported PDO attribute.

Solution: There are two options to resolve this issue:

  1. Downgrade PHP version: You can downgrade your PHP version to an earlier release, such as 8.1.21 or 8.2.8, to avoid the bug. However, it’s important to ensure that you prevent automatic updates to prevent the issue from recurring.

  2. Temporarily disable PDO attribute: You can temporarily disable the affected PDO attribute, PDO::ATTR_STRINGIFY_FETCHES, in the following two locations:

    • In the sqlsrv section of config/database.php, add the following line:

      'options' => [
          PDO::ATTR_STRINGIFY_FETCHES => false,
      ],
      
    • In the $options variable of vendor/laravel/framework/src/Illuminate/Database/Connectors/SqlServerConnector.php, add the following line:

      $options[PDO::ATTR_STRINGIFY_FETCHES] = false;
      

Remember to revert these changes once the fixed sqlsrv extension is released.

Q&A

What caused the issue?

Fixed in PHP 8.1.22 and 8.2.9 to fix a MySQL issue which uncovered a bug in sqlsrv extension.

How to fix it temporarily?

Comment out PDO::ATTR_STRINGIFY_FETCHES in sqlsrv section of config/database.php.

How to fix it permanently?

Upgrade to the fixed sqlsrv extension version 5.11.1 when it is released.

Video Explanation:

The following video, titled "How to Build a REST API With Laravel: PHP Full Course - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Download Laravel scripts: ...