这是我第一次将 php 与 SQL Server 集成。上下文中的 SQL Server 位于同一 LAN 上的另一台物理计算机上。我正在使用下面解释的配置:
- php:版本 5.5
- SQL Server:2005 Express
- php 驱动程序:sqlsrv 3.2
- 线程安全:已禁用
我遇到以下错误:
Connection could not be established.
Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => -49 [code] => -49 [2] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 [message] => This extension requires the Microsoft SQL Server 2012 Native Client. Access the following URL to download the Microsoft SQL Server 2012 Native Client ODBC driver for x86: http://go.microsoft.com/fwlink/?LinkId=163712 ) [1] => Array ( [0] => IM002 [SQLSTATE] => IM002 [1] => 0 [code] => 0 [2] => [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 ) ).
使用的代码如下:
<?php
$serverName = "TESTSERV\SQLEXPRESS"; //serverName\instanceName
$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 的 Microsoft SQL Server 驱动程序需要安装 Microsoft SQL Server 2012 Native Client,您可以在错误消息中看到。仅下载扩展 dll 是不够的。更多内容可以阅读官方System Requirements (Microsoft Drivers for PHP for SQL Server) .
Versions 3.2 and 3.1 require Microsoft ODBC Driver 11 (or higher) for SQL Server. To download the Microsoft ODBC Driver 11 for PHP for SQL Server, see Microsoft ODBC Driver 11 for SQL Server.
If you are using the SQLSRV driver, sqlsrv_client_info will return information about which version of SQL Server Native Client is being used by the Microsoft Drivers for PHP for SQL Server. If you are using the PDO_SQLSRV driver, you can use PDO::getAttribute to discover the version.
https://www.microsoft.com/en-us/download/details.aspx?id=36434
下载、安装并尝试刷新您的测试页面。
关于php - php与sql server集成的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30510938/