PHP Sql Server PDOException:could not find driver
Asked Answered
S

2

9

My server is a Windows 2008 server. PHP Version 7.2.7 is installed and running. Sql Server 11 (64 bit) is installed and is working (there is a couple asp.net apps running and already using that database)

I downloaded the PHP Sql Server Drivers from Microsofts website and placed the .dll files in the PHP ext directory.

In my PHP.ini I added:extension=php_pdo_sqlsrv_7_nts_x64

In my .php file I am using to test my db connection I have:

 $SqlServer = "THISSERVER\SQLEXPRESS";
 $SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword"); 
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";}     

I am getting: PHP Fatal error: Uncaught PDOException: could not find driver in D:\Inetpub\wwwroot\TechStory2\DBtest.php:7 (Line 7 is the $SqlServerCon line).

What did I do wrong? and What do I need to do to get a connection to Sql Server?

Spheroidicity answered 20/7, 2018 at 19:27 Comment(2)
i think you need to put the .dll at the end of the extension name in your ini. remember to restart the web server after you change this.Inviting
What @Inviting said. On Windows we will assume you did not compile from source and the PDO extension (php_pdo.dll) is built into your binary. If not, make sure it is loaded before SQLSRV.Ruffle
S
5

I got it figured out. I had to install the ODBC Driver 17 for SQL Server (msodbcsql_17.2.0.1_x64.msi) on my server. The SQL Server Native Client 11.0 was installed but not the ODBC Driver for SQL Server.

For future reference for anyone else with this or a similar issue...

It can be downloaded at https://www.microsoft.com/en-us/download/details.aspx?id=56567 (note: if you have a 32 bit server, you will want to install the msodbcsql_17.2.0.1_x86.msi - If you accidentally try to install the incorrect version, it will let you know during the installation). After the driver is installed, you need to reboot the server. It won't prompt you to restart, but you'll need to.

In my PHP.ini I have added extension=php_pdo_sqlsrv_72_nts.dll and extension=php_sqlsrv_72_nts_x64.dll They can be downloaded at https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

More info can be found at https://learn.microsoft.com/en-us/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 and https://learn.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

I can now establish a connection to Sql Server using either sqlsrv_connect or PDO.

PDO connection test:

$SqlServer = "THISSERVER\SQLEXPRESS";
$SqlServerCon = new PDO("sqlsrv:server=$SqlServer;Database=TheDatabase", "DbUName", "DbPassword"); 
if (!$SqlServerCon) {die('Unable To Connect to Sql Server');}
else
{echo "Connection Successful";} 

sqlsrv_connect connection test:

$SqlServer = "THISSERVER\SQLEXPRESS";
$DbConnInfo = array( "Database"=>"TheDatabase", "UID"=>"DbUName", "PWD"=>"DbPassword");
$SqlServerCon = sqlsrv_connect( $SqlServer, $DbConnInfo);
if( $SqlServerCon ) {echo "Connection established";}
else
{echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));}
Spheroidicity answered 25/7, 2018 at 15:30 Comment(2)
there is also new library from M$ github.com/Microsoft/msphpsql/tree/PHP-7.0Devinna
Can I clear up what you’re saying here? (a) Is the SQL server on the same machine as your web server? (b) Did you need to install the ODBC driver as well as the other driver?Interlace
B
-1

As commentators said - adding .dll at the end of extension= config line should be good start with your issue.

I can also see that you're trying to load NTS version of extension (NTS stands for non thread safe). Are you sure that you're going to load right version of extension? Please check if you're running PHP as NTS runtime or not - you can check it with phpinfo();.

Bucaramanga answered 25/7, 2018 at 8:59 Comment(1)
Yes, NTS since I am using fastcgi on a Windows server.Spheroidicity

© 2022 - 2024 — McMap. All rights reserved.