【问题标题】:connect to mssql database via sqlsrv_connect() - PHP通过 sqlsrv_connect() 连接到 mssql 数据库 - PHP
【发布时间】:2014-12-05 03:10:47
【问题描述】:

我正在尝试使用 sqlsrv_connect() 函数使用 PHP 连接到 2005 Microsoft sql 数据库。唉,这个函数返回false,我不知道为什么。

<?php
$myServer = "MAZE.jpc.wa.edu.au.local";
$myUser = "myUsername";
$myPass = "myPassword";
$myDB = "John Paul College";
$connectionInfo = array("Database"=>$myDB, "UID" => $myUser, "PWD" => $myPass);

$conn = sqlsrv_connect($myServer, $connectionInfo); //returns false
if( $conn === false )
{
    echo "failed connection";
}

$sql = "SELECT name FROM users WHERE name= 'admin'";
$stmt = sqlsrv_query($conn,$sql);
if(sqlsrv_fetch($stmt) ===false)
{
    echo "couldn't fetch data"; 
}
$name = sqlsrv_get_field($stmt,0);
echo $name;
sqlsrv_close( $conn );
?>

有人知道我为什么连接不上吗? 谢谢。

编辑。

好的,好吧,由于其他人的回答,我能够提出错误消息,其中指出

Array (
    [0] => Array (
        [0] => IMSSP [SQLSTATE] => IMSSP
        [1] => -49 [code] => -49
        [2] => This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later)
                or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server.
                Neither of those ODBC Drivers are currently installed.
                Access the following URL to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver
                for x86: http://go.microsoft.com/fwlink/?LinkId=163712 
        [message] => This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later) 
              or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server.
              Neither of those ODBC Drivers are currently installed. 
              Access the following URL to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver 
              for x86: http://go.microsoft.com/fwlink/?LinkId=163712 )
    [1] => Array (
        [0] => IM002 [SQLSTATE] => IM002 
        [1] => 0 [code] => 0 
        [2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified 
            [message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )

我不完全确定如何解决此问题。我使用 XAMPP 作为我的测试网络服务器、php 版本 5.3 和以下 .dll php_sqlsrv_53_ts_vc6.dllphp_pdo_sqlsrv_53_ts_vc6.dll

【问题讨论】:

  • 你得到的确切错误是什么?
  • 您的数据库是否名为John Paul College?与所有这些空间?那么我认为你应该使用$myDB = "[John Paul College]";
  • 您需要按照错误消息中的说明下载并安装驱动程序。它几乎解释了如何解决您的问题?
  • 不是明确说明您需要SQL Server 2008 或更高版本吗?如果您仍在使用 SQL Server 2005,请尝试使用 mssql
  • 很遗憾,我没有升级服务器的选项。我被 2005 年困住了。如果我目前使用错误的驱动程序,有谁知道我应该使用哪些驱动程序?

标签: php sql-server sqlsrv


【解决方案1】:

我建议您使用 sqlsrv_errors() 显示连接错误。请看下面的实现。

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

欲了解更多信息:http://php.net/manual/en/function.sqlsrv-connect.php

【讨论】:

    【解决方案2】:

    按照错误消息的提示下载 SQL Native Client。这与 SQL Server 不同,并且需要此驱动程序才能正常工作。在 IIS 环境中构建 sqlsrv 实现的经验教训

    【讨论】:

      猜你喜欢
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2013-06-09
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多