致命错误:未捕获错误:调用未定义函数sqlsrv_connect()

时间:2017-11-03 19:47:57

标签: php sql apache pdo php-7

我正在尝试连接到我们可以通过" Microsoft SQL Server Management Studio访问的SQL Server":

enter image description here

我尝试了很多我在SO上看过的不同的东西,但没有任何效果。在linkCollections: function(callback){ var owners = Owner.find().cursor(); owners.on('data', function(owner){ Book.findOne({'bookName': owner.bookName}, function(err, book){ if (err) { console.log(err); }else { if ( book !== null ) { //do stuff to book } } }); Author.findOne({'author': owner.bookAuthor}, function(err, author){ if (err) { console.log(err); }else { if ( author !== null ) { //do stuff to author } } }); }).on('error', function(err){ console.log('error retrieving records'); }).on('close', function(){ callback(true); }); } 页面上运行以下代码时出现错误Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:\Apache24\htdocs\test.php:52 Stack trace: #0 {main} thrown in C:\Apache24\htdocs\test.php on line 52

test.php

我的php.ini文件已经启用了许多不同的扩展,但没有一个有效。我已经尝试了以下所有方法:

$serverName = "SERVER.DOMAIN.COM, 1433"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"DB_NAME", "UID"=>"USER", "PWD"=>"PASS");
$ma_conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $ma_conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

目前我有:

extension = php_sqlsrv_56_ts.dll
extension=php_sqlsrv_55_ts.dll
extension=php_sqlsrv_55_nts.dll
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll

在CMD中使用Apache和PHP的计算机上,我得到以下结果:

extension_dir = "c:\apache24\php7\ext\"
extension=php_pdo.dll
extension=php_pgsql.dll
[PHP_SQLSRV]
extension=php_sqlsrv_54_ts.dll
[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_54_ts.dll

我还在Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved. C:\Windows\system32>php -c c:\apache24\php7\php.ini -v -display_startup_errors=1 PHP Warning: PHP Startup: Unable to load dynamic library 'c:\apache24\php7\ext\ php_pdo.dll' - %1 is not a valid Win32 application. in Unknown on line 0 PHP Warning: PHP Startup: Unable to load dynamic library 'c:\apache24\php7\ext\ php_sqlsrv_54_ts.dll' - %1 is not a valid Win32 application. in Unknown on line 0 PHP Warning: PHP Startup: Unable to load dynamic library 'c:\apache24\php7\ext\ php_pdo_sqlsrv_54_ts.dll' - %1 is not a valid Win32 application. in Unknown on line 0 PHP 7.0.10 (cli) (built: Aug 18 2016 09:48:53) ( ZTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies C:\Windows\system32> 文件夹中下载并运行了SQLSRV32.EXE

我有:

  • Windows Server 2012 RS win64
  • Apache 2.4.23 win64
  • PHP 7.0.10 win64
  • Microsoft SQL Server 2012 Native Client win64
  • Microsoft Visual C ++ 2012 x64&amp; x86 Redistributables(两个不同)
  • Microsoft Visual C ++ 2015 Redistributables(x64&amp; x86) - 14.0.23026
  • 用于SQL Server的Microsoft ODBC驱动程序11

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

符合php.ini(php_sqlsrv_XX_ts.dll)中的尝试列表,您已激活PHP v5.5和v5.6的扩展。但是,由于您运行的是PHP 7.0,因此需要下载并激活相应的Microsoft Drivers for PHP for SQL Server。您似乎需要下载驱动程序版本4.0 。这在php.net page for SQLSRV extension上说明。

  

Microsoft支持SQLSRV扩展,可用于   在这里下载:http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

您可以在此处找到Loading the PHP SQL Driver的说明。您可能会收到php_sqlsrv_7_XX.dllXX之类的文件:“ts”或“nts”(线程安全或非线程安全)。我认为你需要线程安全版本。

否则,使用MS SQL Server (PDO) PDO驱动程序(阅读链接中的“安装”部分!)和PDO_SQLSRV DSN(连接字符串)和PDO常规功能,例如所有司机都很常见。该驱动程序将作为extension=php_pdo_sqlsrv_7_ts.dll添加到php.ini中(请查看相同的页面以获取说明:Loading the PHP SQL Driver)。如果你问我,我真的会选择这个选项。

Nota Bene:在配置中的每次更改后,不要忘记重新启动Web服务器和/或数据库服务!

祝你好运。