首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过PHP连接到msSQL数据库

无法通过PHP连接到msSQL数据库
EN

Stack Overflow用户
提问于 2009-01-22 17:01:10
回答 9查看 47.6K关注 0票数 4

我正在使用当前代码尝试访问msSQL 2005数据库:

代码语言:javascript
复制
<?php
$myServer = "[server]";
$myUser = "[username]";
$myPass = "[password]";
$myDB = "[db]";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

它返回以下内容:

代码语言:javascript
复制
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: XXXXXXX in D:\xxxxx.xxx\xxxx.php on line 16
Couldn't connect to SQL Server on XXXXXXX

你认为问题出在哪里?

EN

回答 9

Stack Overflow用户

发布于 2009-01-22 17:19:02

在我看来,您的某个DLL版本不正确。在从SQL2000迁移到SQL2005的过程中出现了一些问题,PHP的创建者并没有自己解决这个问题。这里有很多关于它的帖子:the following link

我相信DLL是ntwdblib.dll,版本至少需要是2000.80.194.0。如果您运行的是Apache或WampServer,则存储Apache的位置有一个相同的dll需要被覆盖。

注意:几天前我遇到了这个问题,找到正确的DLL和覆盖都允许它工作。

另外:您可能需要设置远程连接。默认情况下,Sql Server2005禁用远程连接。您可以通过运行SQL外围应用配置器实用程序来允许远程连接。

票数 5
EN

Stack Overflow用户

发布于 2009-01-22 17:12:37

尝试调用mssql_get_last_message()以获取最后一个错误消息:

代码语言:javascript
复制
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());
票数 4
EN

Stack Overflow用户

发布于 2014-03-08 02:31:17

停止使用

mssql_connect

并开始使用

sqlsrv_connect

这会帮你省去很多麻烦。此外,函数*mssql_connect*已被弃用。

要使用sqlsrv_connect,必须下载驱动程序并将其作为扩展安装,以便识别sqlsrv函数。从微软下载中心下载驱动程序(搜索"sql server php driver"),在这篇文章中,下载地址是:http://www.microsoft.com/en-us/download/details.aspx?id=20098

安装的正确步骤由微软自己在http://www.microsoft.com/en-us/download/details.aspx?id=20098上清楚地解释

  1. 下载驱动程序。2.将其放在PHP ext文件夹中。3.更新php.ini 4.重新启动服务器。

安装完sql server驱动程序后,只需按照http://www.php.net/manual/en/function.sqlsrv-connect.php的说明进行操作即可。下面是一个简短的代码片段:

代码语言:javascript
复制
<?php
$serverName = "serverName\sqlexpress"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"dbName");
$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));
}
?>

投赞成票!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/469964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档