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

4

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

<?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);
?>

它返回以下内容:
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

你认为问题出在哪里?
9个回答

5
我听起来好像是你的DLL版本错误。在从SQL2000到SQL2005的迁移中,PHP的创建者没有解决某些问题。这里有各种关于此的帖子:以下链接 我相信DLL是ntwdblib.dll,版本至少需要为2000.80.194.0。如果您正在运行Apache或WampServer,则需要在存储Apache DLL的位置上覆盖一个相同的dll。
注意:我几天前遇到了这个问题,并找到了正确的DLL并覆盖它可以使它工作。
另外:您可能需要设置远程连接。Sql Server 2005默认禁用远程连接。您可以通过运行SQL Surface Area Configuration实用程序来允许远程连接。

我支持这个提议,你可以查看这个链接:http://bugs.php.net/bug.php?id=40034 - Jon Onstott

4

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

$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());

4
mssql_get_last_message()函数获取SQL服务器发送的最后一条消息。如果无法连接,则没有消息。这不会帮助解决连接错误,如“密码错误”,“未知主机”或“未知用户”等问题。但实际上,mssql_get_last_message()在连接尝试时返回空值是很常见的。 - changokun

1
停止使用mssql_connect,开始使用sqlsrv_connect,这将节省你很多麻烦。另外,函数*mssql_connect*已被弃用。
要使用sqlsrv_connect,必须下载驱动程序并将其安装为PHP的扩展名,以便识别sqlsrv函数。从Microsoft Download Center(搜索“sql server php driver”)下载驱动程序,在本文发布时,下载网址为:http://www.microsoft.com/en-us/download/details.aspx?id=20098
正确的安装步骤由Microsoft自己在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中的说明进行操作。以下是一个快速示例:
<?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));
}
?>

投票赞成!


当调试器遇到 sqlsrv_connect( $serverName, $connectionInfo) 时崩溃了。我还发布了一个相关的查询,链接为 http://stackoverflow.com/questions/32270877/what-it-takes-a-php-application-to-connect-to-mssql-server 。导致崩溃的原因是什么? - Suhail Gupta
mysql_connect()已被弃用,不是mssql_connect()。 - user3465521
mssql_connect()没有被弃用。 - Developer
我不得不在win 2012 r2服务器上安装x64 https://www.microsoft.com/en-us/download/details.aspx?id=50420。 - Robot70

0
晚回复,但可能对某些人有帮助。 我们已经接近成功了,问题在于您的连接参数, 可能需要将服务器名称设置为IP:端口 servername:: MS SQL服务器。例如:hostname:port(Linux)或hostname,port(Windows)。 服务器名称应为“server\instance” 实例只是特定于不同于1433的端口地址...因此只需发现mssql服务器上的端口,然后尝试使用ip:port进行连接。
以下是我完全正常工作的示例代码:
    ini_set('display_errors', '1');
    // $myServer = "winsrv22.somedns.co.uk:22320";//this style works as well
    $servername = "123.21.47.23:22320";
    $myUser = "UserName";
    $myPass = 'xxxxxxxx';
    $myDB = "[database]"; 

    //connection to the database
    $dbhandle = mssql_connect($servername, $myUser, $myPass)
        or die("Couldn'tt connect to SQL Server on $myServer"); 
    if($dbhandle) {
     echo "Success, Connected\r\n";
    } else {
        echo "problem :( \r\n";
    }

希望这能帮助到某些人 :) 编程愉快!!

0

0

无效的凭据,如果您不使用本地主机,请确保将服务器的IP地址添加到安全列表中。


0

几个月前我遇到了一些困难,我发现使其工作的唯一方法是在指定服务器时包括实例名称。例如:

$myServer = "SERVER\INSTANCENAME";

指定仅服务器将无效,即使启用了TCP/IP也是如此。

0
首先尝试在浏览器上执行像 phpinfo() 这样的操作,然后查看哪些数据库是被允许的。
如果你没有看到任何类似于 mssql 的东西,那么它可能没有配置。所以使用 odbc_connect(),它会被配置好...你的连接字符串应该是这样的:
$server = '';
$user = '';
$password = '';
$database = '';
$connection = odbc_connect("Driver={SQL Server Native Client `11.0};Server=$server;Database=$database;", $user, $password);`

如果您正在使用mssql 2005或2008,则将d 11.0更改为10.0,对于其他版本,只需更改为您的mssql版本即可。

-1

如果您使用的是Windows 10。

步骤1. 点击开始按钮

步骤2. 输入“服务”并按回车键(打开应用程序服务)

步骤3. 找到 - SQL Server(MSSQLSERVER)

步骤4. 右键单击 ->选择属性

步骤5. 弹出窗口。您选择第二个选项卡(登录)

步骤6. 选择本地系统帐户

步骤7. 并勾选允许服务与桌面交互

步骤8. 最后点击确定。

刷新一次 现在已连接。 希望这非常有用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接