2014-12-05 82 views
2

我试图通过使用sqlsrv_connect()函数连接到使用PHP的2005 Microsoft SQL数据库。唉,函数返回false,我不知道为什么。通过sqlsrv_connect()连接到mssql数据库 - PHP

<?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作为我的测试Web服务器,PHP版本5.3及以下的.dll php_sqlsrv_53_ts_vc6.dllphp_pdo_sqlsrv_53_ts_vc6.dll

+1

您遇到的确切错误是什么? – 2014-12-05 03:13:18

+0

是你的数据库名为'约翰保罗学院'?与所有这些空间?那么我认为你应该使用'$ myDB =“[John Paul College]”;' – bansi 2014-12-05 03:15:46

+0

您需要下载并安装驱动程序,如错误消息所述。它几乎解释了如何解决你的问题? – Doon 2014-12-05 03:56:49

回答

4

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

0

下载SQL本机客户端的错误信息提示。这与SQL Server是分开的,并且需要此驱动程序正常工作。从在IIS环境中构建sqlsrv的实施中学到的经验教训