I'm trying to connect to my SQL Server database hosted on a Linux VM. I'm running xampp on my development windows machine, and the connection is from a php site I'm building. I think I need to use sqlsrv
to connect. I downloaded the dll sql-server-ver15&viewFallbackFrom=sql-server-2019## from https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view= #
xamppphpetc directory. I also verified that the extension directory in the php.ini file is
extension_dir="C:xamppphpext"
Dynamic Extensions section of php.ini:
extension=php_sqlsrv_81_ts_x86.dll extension=php_pdo_sqlsrv_81_ts_x86.dll extension=php8ts.dllI found information online about removing the
php_ prefix, removing the
.dll suffix, using ts or not ts, moving all files to the extension directory, only the ones listed above Several files were moved to the directory information, excluding
php8ts. dll etc. I have tried every configuration above, both logical and illogical.
$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 tried multiple different connection examples. By doing the above, I get this error:
Fatal Error: Uncaught PDOException: Driver not found in C:xampphtdocssiteindex.php:123 Stack trace: #0 C:xampphtdocssiteindex.php(123): PDO ->__construct('sqlsrv:Server= my_server_ip', 'username', 'password') #1 {main} throws in C:xampphtdocssiteindex.php on line 123From here I logically thought okay, let's check by echoing
phpinfo(); . There is nothing listed for
sqlsrv or PDO variants anywhere in the list. Even if you ctrl f on the sqlsrv page, you can only find the above error.
Other things I've tried is using
sqlsrv_connect instead of PDO. I found conflicting information regarding my php version (8.1) but thought I'd give it a try anyway. However, when I encounter that variant I get:
Fatal error: Calling undefined function sqlsrv_connect()To me it's obvious that my .dll isn't being recognized or something like that. Yet for the life of me I can't understand why. I've verified everything is running as administrator, restarted xampp multiple times, deleted/redownloaded the dlls, etc.
Can anyone point out any obvious issues that I might not have thought of?
P粉7980104412023-11-10 19:58:28
solved. There are two questions.
My Xampp installation is corrupted. It doesn't recognize any of the new DLLs I noticed when using echo phpinfo();
on the page.
For some reason the ini file configuration tries to use x86 dlls instead of x64, even though the extension is written in the ini as x64.
Solution steps:
I reinstalled and reconfigured Xampp again using default settings and now the dll is recognized.
I removed the x86 dll from Xampp's extensions directory.
The final php.ini is like this:
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
Using the final .dll may be redundant, but it will still work as expected. Make sure to verify the correct version of sqlsrv or pdo_sqlsrv as there may be compatibility issues with your version of php.