PHP Sql Server PDOException:找不到驱动程序 [英] PHP Sql Server PDOException:could not find driver

查看:108
本文介绍了PHP Sql Server PDOException:找不到驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器是Windows 2008服务器. PHP版本7.2.7已安装并正在运行. Sql Server 11(64位)已安装并且正在运行(有几个运行并且已经在使用该数据库的asp.net应用程序)

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)

我从Microsoft网站下载了PHP Sql Server驱动程序,并将.dll文件放在PHP ext目录中.

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

在我的PHP.ini中,我添加了:extension=php_pdo_sqlsrv_7_nts_x64

In my PHP.ini I added:extension=php_pdo_sqlsrv_7_nts_x64

在我的.php文件中,我用来测试我的数据库连接:

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";}     

我得到: PHP Fatal error: Uncaught PDOException: could not find driver in D:\Inetpub\wwwroot\TechStory2\DBtest.php:7(第7行是$ SqlServerCon行).

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).

我做错了什么?和我需要做些什么才能获得与Sql Server的连接?

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

推荐答案

我明白了.我必须在服务器上安装用于SQL Server的ODBC驱动程序17(msodbcsql_17.2.0.1_x64.msi).已安装SQL Server Native Client 11.0,但未安装SQL Server的ODBC驱动程序.

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...

可以从 https://www下载. microsoft.com/en-us/download/details.aspx?id=56567 (注意:如果您有32位服务器,则需要安装msodbcsql_17.2.0.1_x86.msi-如果您不小心尝试了安装不正确的版本,则会在安装过程中通知您).安装驱动程序后,需要重新引导服务器.它不会提示您重新启动,但是您需要这样做.

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.

在我的PHP.ini中,我添加了extension=php_pdo_sqlsrv_72_nts.dllextension=php_sqlsrv_72_nts_x64.dll,它们可以在

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://docs.microsoft.com/en-us/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

更多信息可以在 https://docs.microsoft.com/zh-cn/sql/connect/php/loading-the-php-sql-driver?view=sql-server-2017 https://docs.microsoft.com/zh-cn/sql/connect/php/system-requirements-for-the-php-sql-driver?view=sql-server-2017

我现在可以使用sqlsrv_connect或PDO建立与Sql Server的连接.

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

PDO连接测试:

$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连接测试:

$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));}

这篇关于PHP Sql Server PDOException:找不到驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆