php mssql_connect() 与 PHP5 和 MSSQL2012 Express
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12282936/
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
mssql_connect() with PHP5 and MSSQL2012 Express
提问by grmihel
I'm having heavy issues trying to connect to my MSSQL 2012 Express database, with PHP5 running on an Apache. I have as a test setup just installed a XAMPP with PHP 5.4.4 and Apache running on a Windows 7 machine.
我在尝试连接到我的 MSSQL 2012 Express 数据库时遇到了严重的问题,PHP5 在 Apache 上运行。作为测试设置,我刚刚在 Windows 7 机器上安装了带有 PHP 5.4.4 和 Apache 的 XAMPP。
My PHP code (phpmssql_genxml.php):
我的 PHP 代码(phpmssql_genxml.php):
$connection = mssql_connect('192.168.40.150', $username, $password);
if (!$connection) { die('Not connected : ' . mssql_get_last_message());}
$db_selected = mssql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mssql_get_last_message());
}
$query = "SELECT * FROM Cust WHERE 1";
$result = mssql_query($query);
if (!$result) {
die('Invalid query: ' . mssql_get_last_message());
}
Output when trying to enter the site:
尝试进入站点时的输出:
Fatal error: Call to undefined function mssql_connect() in C:\xampp\htdocs\phpmssql_genxml.php on line 13
Even if I try to hardcode the username and password into the string, I still get the same result. Have search a lot on google, but havn't found that post that fixed my issue yet :/ Have enable TCP/IP for the DB instance pipe, even try'd to assign a specific TCP port for it. Have created a rule in the Win7 firewall allowing all traffic to the standard port 1433. Still no luck.
即使我尝试将用户名和密码硬编码到字符串中,我仍然得到相同的结果。在谷歌上搜索了很多,但还没有找到解决我问题的帖子:/为数据库实例管道启用 TCP/IP,甚至尝试为其分配特定的 TCP 端口。在 Win7 防火墙中创建了一个规则,允许所有流量到标准端口 1433。仍然没有运气。
any1 have an idea?? What does the 'Fatal error' part means? Is it the Apache error, PHP or a Database error when trying to connect to it??
有什么想法吗??“致命错误”部分是什么意思?尝试连接时是 Apache 错误、PHP 还是数据库错误??
采纳答案by eis
You are missing MSSQL driver from your PHP setup. Download it from here, assuming you have the required system configuration mentioned on the page.
您的 PHP 设置中缺少 MSSQL 驱动程序。从这里下载它,假设您有页面上提到的所需系统配置。
Setting up, from their instructions:
根据他们的说明进行设置:
- Download SQLSRV30.EXE to a temporary directory
- Run SQLSRV30.EXE
- When prompted, enter the path to the PHP extensions directory
- After extracting the files, read the Installation section of the SQLSRV30_Readme.htm file for next steps
- 将 SQLSRV30.EXE 下载到临时目录
- 运行 SQLSRV30.EXE
- 出现提示时,输入 PHP 扩展目录的路径
- 解压文件后,阅读 SQLSRV30_Readme.htm 文件的安装部分以了解后续步骤
I would also recommend using standard Apache + PHP installation, if you plan to work with MSSQL, instead of any *AMP package.
如果您打算使用 MSSQL,我还建议使用标准的 Apache + PHP 安装,而不是任何 *AMP 包。
回答by greaterKing
You don't need to use SQLSRV30 as this was not the solution that I needed though some may find it useful. I have a local dev environment running XAMPP + php5.5.9 + MSSQL2012 and I needed to use mssql functions because thats what we use at work on our servers. So I use freetds and "luckily" found this:
您不需要使用 SQLSRV30,因为这不是我需要的解决方案,尽管有些人可能会发现它很有用。我有一个运行 XAMPP + php5.5.9 + MSSQL2012 的本地开发环境,我需要使用 mssql 函数,因为那是我们在服务器上工作时使用的函数。所以我使用 freetds 并“幸运地”发现了这个:
https://moodle.org/mod/forum/discuss.php?d=232844
https://moodle.org/mod/forum/discuss.php?d=232844
where one of the users already compiled the php_dblib.dll TS and NTS for my php version suffice to say I have everything working and using all the MSSQL functions that our dev team is used to. The extension for mssql support can always be compiled.
其中一位用户已经为我的 php 版本编译了 php_dblib.dll TS 和 NTS,足以说明我一切正常,并使用了我们开发团队习惯的所有 MSSQL 函数。mssql 支持的扩展总是可以编译的。

