'XAMPP/SQLSRV: Unable to find Sqlsrv in PHPINFO(); - errors coming from connection

I'm attempting to connect into a SQL Server DB I have hosted on my Linux VM. I'm running xampp on my development windows machine and the connection is coming from a php site I'm building. I figured I'd need to use sqlsrv to connect in. I downloaded the dll's from https://docs.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15&viewFallbackFrom=sql-server-2019

I've moved the necessary dll files to my xampp\php\etc\ directory. I've also verified the extension directory in the php.ini file is extension_dir="C:\xampp\php\ext"

The following have been added to the Dynamic Extensions section of the php.ini:

extension=php_sqlsrv_81_ts_x86.dll
extension=php_pdo_sqlsrv_81_ts_x86.dll
extension=php8ts.dll

I've found info online about removing the php_ prefix, removing the .dll suffix, using ts or non ts, moving all files into the extensions directory, moving only the couple listed above into the directory, not including php8ts.dll, etc. I've tried every configuration of the above, both logical and illogical.

Here's a sample connection code for my site:

$conn = new PDO('sqlsrv:Server=my_server_ip\\MSSQLSERVER;Database=dbname', 'username', 'password');
if ($conn === false) {
    echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
    exit;
} else {
    echo "success";
}

I've tried multiple different connection examples. With the one above, I receive this error:

Fatal error: Uncaught PDOException: could not find driver in C:\xampp\htdocs\site\index.php:123 Stack trace: #0 C:\xampp\htdocs\site\index.php(123): PDO->__construct('sqlsrv:Server=my_server_ip', 'username', 'password') #1 {main} thrown in C:\xampp\htdocs\site\index.php on line 123

From here I logically thought okay, let's check phpinfo(); by echoing it. There is nothing at all listed for sqlsrv or the PDO variant anywhere on the list. Even ctrl+f on the page for sqlsrv, the only thing that is found is the error above.

I have verified I have the ODBC drivers installed.

enter image description here

The other things I've tried was to use sqlsrv_connect instead of PDO. I found conflicting information on this working for my php version (8.1), but figured let's try it anyway. However when I run into that variant, I get:

Fatal error: Call to undefined function sqlsrv_connect()

It seems obvious to me my .dlls are not being recognized or something of the sort. However I cannot for the life of me understand why. I've verified everything is ran as admin, restarted xampp multiple times, removed/redownloaded the dlls, etc.

Can anyone point out any glaring problems I may not be thinking of?



Solution 1:[1]

Solved. There were two problems.

  1. My Xampp installation was corrupt. It wouldn't recognize ANY new DLLs I noticed from using echo phpinfo(); on my page.

  2. The ini file configuration was attempting to use the x86 dll's instead of the x64's for some reason, even when the extensions were written in the ini as x64.

Steps to solve:

  1. I reinstalled and reconfigured Xampp with default settings once more, and dll's were now recognized.

  2. I removed the x86 dll's from the extensions directory of Xampp.

The final php.ini is as such:

extension=php_sqlsrv_81_ts_x64.dll
extension=php_pdo_sqlsrv_81_ts_x64.dll
extension=php8ts.dll
extension=php_pdo_odbc.dll
extension=php_pdo.dll

Utilizing the final .dll may be redundant, but it still works as intended. Make sure you verify the correct version of sqlsrv or pdo_sqlsrv, as compatibility issues with your phpversion may occur.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Melvin Rowls