无法从 PHP 连接到 SQL 服务器
Can't connect to SQL Server from PHP
我用XAMPP。我需要建立从 PHP 代码到我的 MS SQL 服务器的连接。因此,我从 here 安装了 sqlsrv
驱动程序。根据 phpinfo()
它被正确加载:
我收到此错误消息:
Connection could not be established. Array ( [0] => Array ( [0] =>
IMSSP [SQLSTATE] => IMSSP => -49 [code] => -49 => This
extension requires the Microsoft ODBC Driver 11 for SQL Server. Access
the following URL to download the ODBC Driver 11 for SQL Server for
x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This
extension requires the Microsoft ODBC Driver 11 for SQL Server. Access
the following URL to download the ODBC Driver 11 for SQL Server for
x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) => Array (
[0] => IM002 [SQLSTATE] => IM002 => 0 [code] => 0 =>
[Microsoft][ODBC Driver Manager] Data source name not found and no
default driver specified [message] => [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified ) )
链接指向 x86 客户端,但我只能安装 64 位版本,这是我从
安装的
https://www.microsoft.com/en-us/download/details.aspx?id=29065
我仍然得到同样的错误。
规格:
OS: Windows Server 2012 64-bit
PHP: 5.6.19
代码就这么简单:
$serverName = "servername";
$connectionInfo = array( "Database"=>"DBname", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
现在呢?我应该在任何 PHP/Apache
配置文件中注册安装的 SQL Native Client 吗?
根据 this blog,Microsoft 的 SQL Server 2012 Feature Pack 不包含 SQL Server 的 ODBC Driver 11;这是 sqlsrv
扩展 3.1 及更高版本所必需的。
我遇到了同样的问题...
使用 phpinfo() 检查您使用的 php 版本,然后下载与您的 php 版本相关的扩展。然后也转到您的 php.ini 文件并像这样添加这些文件
extension=<extension>.dll
然后我一直遗漏并强调我的一个重要步骤是您需要下载下面 link 中找到的驱动程序...否则您的 php 脚本将无法连接到您的SQL服务器....下载并安装在实际安装了SQL服务器的机器上...如果它是远程服务器,则必须安装在远程服务器上...如果它在你的本地机器上,这必须安装在你的本地机器上...
https://www.microsoft.com/en-us/download/details.aspx?id=36434
那你就可以开始了...
我用XAMPP。我需要建立从 PHP 代码到我的 MS SQL 服务器的连接。因此,我从 here 安装了 sqlsrv
驱动程序。根据 phpinfo()
它被正确加载:
我收到此错误消息:
Connection could not be established. Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP => -49 [code] => -49 => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft ODBC Driver 11 for SQL Server. Access the following URL to download the ODBC Driver 11 for SQL Server for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) => Array ( [0] => IM002 [SQLSTATE] => IM002 => 0 [code] => 0 => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )
链接指向 x86 客户端,但我只能安装 64 位版本,这是我从
安装的https://www.microsoft.com/en-us/download/details.aspx?id=29065
我仍然得到同样的错误。
规格:
OS: Windows Server 2012 64-bit
PHP: 5.6.19
代码就这么简单:
$serverName = "servername";
$connectionInfo = array( "Database"=>"DBname", "UID"=>"username", "PWD"=>"password");
$conn = sqlsrv_connect($serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
现在呢?我应该在任何 PHP/Apache
配置文件中注册安装的 SQL Native Client 吗?
根据 this blog,Microsoft 的 SQL Server 2012 Feature Pack 不包含 SQL Server 的 ODBC Driver 11;这是 sqlsrv
扩展 3.1 及更高版本所必需的。
我遇到了同样的问题...
使用 phpinfo() 检查您使用的 php 版本,然后下载与您的 php 版本相关的扩展。然后也转到您的 php.ini 文件并像这样添加这些文件
extension=<extension>.dll
然后我一直遗漏并强调我的一个重要步骤是您需要下载下面 link 中找到的驱动程序...否则您的 php 脚本将无法连接到您的SQL服务器....下载并安装在实际安装了SQL服务器的机器上...如果它是远程服务器,则必须安装在远程服务器上...如果它在你的本地机器上,这必须安装在你的本地机器上...
https://www.microsoft.com/en-us/download/details.aspx?id=36434
那你就可以开始了...