php 致命错误:调用未定义的函数 sqlsrv_connect()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22015179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Call to undefined function sqlsrv_connect()
提问by Sarah
I have come across quite a few post regarding the same subject question, however I am still unable to resolve it and so I ask. I am trying to connect to sql in my php script. My connection string is:
我遇到过很多关于同一主题问题的帖子,但是我仍然无法解决它,所以我问。我正在尝试在我的 php 脚本中连接到 sql。我的连接字符串是:
/* Specify the server and connection string attributes. */
$serverName = "xxx-PC\SQLExpress";
$connectionOptions = array("Database"=>"Salesforce");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
I have installed and included the following in my php.inifile located under the wamp folder: C:\wamp\bin\php\php5.4.16:
我已在php.ini位于 wamp 文件夹下的文件中安装并包含以下内容C:\wamp\bin\php\php5.4.16:
extension=c:/wamp/bin/php/php5.4.16/ext/php_sqlsrv_53_ts.dll
My wampserveris running fine and so are the wampapacheand wampsqldservices. I am able to execute php.exe successfully. However I am unable to make the connection to SQL Server 2008 R2where my database is located. Please help!
我wampserver的运行良好wampapache,wampsqld服务也运行良好。我能够成功执行 php.exe。但是我无法连接到SQL Server 2008 R2我的数据库所在的位置。请帮忙!
EDIT 1: The wamp server is running the wampmysql service while I am trying to connect to SQL Server 2008 R2. Could this be the reason? Should I be using MySQLinstead of SQL? Any pointers?
编辑 1:当我尝试连接到SQL Server 2008 R2. 这可能是原因吗?我应该使用MySQL而不是SQL吗?任何指针?
EDIT 2: I do not see sqlsrvsection at all when I run phpinfo()though I have added extension=php_sqlsrv_54_ts.dllin the php.inifile located in the bin folder of the wamp server.
编辑 2:虽然我已经添加了位于 wamp 服务器的 bin 文件夹中的文件,但我在sqlsrv运行时根本看不到该部分。phpinfo()extension=php_sqlsrv_54_ts.dllphp.ini


采纳答案by Sarah
Thishelped me get to my answer. There are two php.inifiles located, in my case, for wamp. One is under the php folder and the other one is in the C:\wamp\bin\apache\Apachex.x.x\binfolder. When connecting to SQL through sqlsrv_connectfunction, we are referring to the php.inifile in the apachefolder. Add the following (as per your version) to this file:
这帮助我得到了答案。就php.ini我而言,有两个文件用于 wamp。一个在php文件夹下,另一个在C:\wamp\bin\apache\Apachex.x.x\bin文件夹中。通过sqlsrv_connect函数连接SQL时,我们指的是php.ini文件apache夹中的文件。将以下内容(根据您的版本)添加到此文件中:
extension=c:/wamp/bin/php/php5.4.16/ext/php_sqlsrv_53_ts.dll
回答by álvaro González
When you install third-party extensions you need to make sure that all the compilation parameters match:
当您安装第三方扩展时,您需要确保所有编译参数都匹配:
- PHP version
- Architecture (32/64 bits)
- Compiler (VC9, VC10, VC11...)
- Thread safety
- PHP版本
- 架构(32/64 位)
- 编译器(VC9、VC10、VC11...)
- 线程安全
Common glitches includes:
常见故障包括:
- Editing the wrong
php.inifile (that's typical with bundles); the right path is shown inphpinfo(). - Forgetting to restart Apache.
Not being able to see the startup errors; those should show up in Apache logs, but you can also use the command line to diagnose it, e.g.:
php -d display_startup_errors=1 -d error_reporting=-1 -d display_errors -c "C:\Path\To\php.ini" -m
- 编辑错误的
php.ini文件(这是捆绑包的典型情况);正确的路径显示在 中phpinfo()。 - 忘记重新启动Apache。
无法看到启动错误;这些应该显示在 Apache 日志中,但您也可以使用命令行来诊断它,例如:
php -d display_startup_errors=1 -d error_reporting=-1 -d display_errors -c "C:\Path\To\php.ini" -m
If everything's right you should see sqlsrvin the command output and/or phpinfo()(depending on what SAPI you're configuring):
如果一切正常,您应该sqlsrv在命令输出和/或phpinfo()(取决于您配置的 SAPI)中看到:
[PHP Modules]
bcmath
calendar
Core
[...]
SPL
sqlsrv
standard
[...]
[PHP 模块]
bcmath
日历
核心
[...]
SPL
sqlsrv
标准
[...]
回答by Elshan
If you are using Microsoft Drivers 3.1, 3.0, and 2.0.
Please check your PHP version already install with IIS.
Use this script to check the php version:
如果您使用的是Microsoft 驱动程序 3.1、3.0 和 2.0。请检查您的 PHP 版本已经安装了 IIS。
使用此脚本检查 php 版本:
<?php echo phpinfo(); ?>
OR
或者
If you have installed PHP Manager in IISusing web platform Installer you can check the version from it.
如果您已经使用 Web 平台安装程序在 IIS 中安装了PHP 管理器,您可以从中检查版本。
Then:
If you are using new PHP version (5.6) please download Drivers from here
For PHP version Lower than 5.6 - please download Drivers from here
然后:
如果您使用的是新的 PHP 版本 (5.6),请从此处下载驱动程序
对于低于 5.6 的 PHP 版本 - 请从此处下载驱动程序
- PHP Driver version 3.1 requires PHP 5.4.32, or PHP 5.5.16, or later.
- PHP Driver version 3.0 requires PHP 5.3.0 or later. If possible, use PHP 5.3.6, or later.
- PHP Driver version 2.0 driver works with PHP 5.2.4 or later, but not with PHP 5.4. If possible, use PHP 5.2.13, or later.
- PHP 驱动程序 3.1 版需要 PHP 5.4.32、PHP 5.5.16 或更高版本。
- PHP 驱动程序 3.0 版需要 PHP 5.3.0 或更高版本。如果可能,请使用 PHP 5.3.6 或更高版本。
- PHP 驱动程序 2.0 版驱动程序适用于 PHP 5.2.4 或更高版本,但不适用于 PHP 5.4。如果可能,请使用 PHP 5.2.13 或更高版本。
Then use the PHP Manager to add that downloaded drivers into php configfile.You can do it as shown below (browse the files and press OK).
Then Restart the IIS Server
然后使用 PHP 管理器将下载的驱动程序添加到php config文件中。您可以按如下所示进行操作(浏览文件并按 OK)。
然后重启IIS服务器


If this method not work please change the php versionand try to run your php script.
如果此方法不起作用,请更改php 版本并尝试运行您的 php 脚本。
Tip:Change the php versionto lower and try to understand what happened.then you can download relevant drivers.
提示:php version把那个改成低一点,试试看是怎么回事。然后就可以下载相关的驱动了。
回答by Dave Child
First check that the extension is properly loaded in phpinfo();(something like sqlsrvshould appear). If not, the extension isn't properly loaded. You also need to restart apache after installing an extension.
首先检查扩展是否正确加载phpinfo();(sqlsrv应该出现类似的东西)。如果不是,则扩展未正确加载。安装扩展后,您还需要重新启动 apache。
回答by ashkufaraz
i have same this because in httpd.conf in apache PHPIniDir D:/wamp/bin/php/php5.5.12that was incorrect
我也有同样的问题,因为在 apache 中的 httpd.conf 中PHPIniDir D:/wamp/bin/php/php5.5.12是不正确的

