通过 sqlsrv_connect() - PHP 连接到 mssql 数据库


connect to mssql database via sqlsrv_connect() - PHP

我正在尝试使用 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 和以下 .dlls php_sqlsrv_53_ts_vc6.dllphp_pdo_sqlsrv_53_ts_vc6.dll

我建议您使用 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

按照错误消息的建议下载 SQL 本机客户端。这与 SQL Server 是分开的,并且是此驱动程序正常工作的要求。在 IIS 环境中构建 sqlsrv 实现的经验教训