【问题标题】:Unable to connect to msSQL database via PHP无法通过 PHP 连接到 msSQL 数据库
【发布时间】:2010-10-02 23:13:44
【问题描述】:

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

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

你认为问题是什么?

【问题讨论】:

    标签: php sql sql-server sql-server-2005 connection


    【解决方案1】:

    如果您使用的是 Windows 10。

    第一步,点击开始按钮

    第二步,输入Service并进入(打开App Services)

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

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

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

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

    步骤 7. 并检查允许服务与桌面交互

    第8步,最后点击确定。

    1次刷新 现在连接。 我希望它非常有用

    【讨论】:

      【解决方案2】:

      在我看来,您的 DLL 之一是错误的版本。从 SQL2000 迁移到 SQL2005 存在某种问题,PHP 的创建者并没有自己解决。这里有各种关于它的帖子:the following link

      我相信 DLL 是 ntwdblib.dll 并且版本至少需要是 2000.80.194.0 版本。如果您正在运行 Apache 或 WampServer,则存储 Apache DLL 的相同 dll 需要被覆盖。

      注意:几天前我遇到了这个问题,找到正确的 DLL 并覆盖两者都可以工作。

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

      【讨论】:

      【解决方案3】:

      回复较晚,但可能对某人有所帮助。 我们快到了,这是您的连接参数的问题, 可能您需要将服务器名称指定为IP:port

      servername:: MS SQL 服务器。例如hostname:port (Linux), or 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";
          }
      

      希望这对某人有所帮助:) 编码愉快!

      【讨论】:

        【解决方案4】:

        在此站点http://www.microsoft.com/en-us/download/details.aspx?id=20098 下载驱动程序,然后打开您的 php.ini 文件并添加您下载的 DLL。

        【讨论】:

          【解决方案5】:

          停止使用

          mssql_connect

          开始使用

          sqlsrv_connect

          这将为您省去很多麻烦。另外,函数 *mssql_connect* 已被弃用。

          要使用 sqlsrv_connect,您必须下载驱动程序并将其安装为 PHP 的扩展,以识别 sqlsrv 函数。从微软下载中心下载驱动(搜索“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 的说明进行操作即可。下面是一个快速的 sn-p:

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

          投票!

          【讨论】:

          【解决方案6】:

          首先尝试在浏览器上执行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 的版本。

          【讨论】:

            【解决方案7】:

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

            $myServer = "SERVER\INSTANCENAME";
            

            即使启用 TCP/IP,仅指定服务器也不起作用。

            【讨论】:

              【解决方案8】:

              尝试调用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());
              

              【讨论】:

              • mssql_get_last_message() 获取 sql server 发送的最后一条消息。如果您无法连接... 没有消息。这无助于错过连接。我们如何找到诸如“密码错误”、“未知主机”或“未知用户”之类的连接错误消息? - edit 实际上,我看到这是连接尝试的标准。那么,当 mssql_get_last_message() 在连接尝试时不返回任何内容是什么意思?
              【解决方案9】:

              无效的凭据,如果您不使用 localhost,请确保将服务器的 ip 添加到安全列表中。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-12-29
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-03-15
                相关资源
                最近更新 更多