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

3

我正在尝试使用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作为我的测试Web服务器,PHP版本为5.3,并使用以下.dll文件:php_sqlsrv_53_ts_vc6.dllphp_pdo_sqlsrv_53_ts_vc6.dll


1
你具体遇到了什么错误? - user1846065
你的数据库名字叫做John Paul College吗?带有所有这些空格吗?那么我认为你应该使用 $myDB = "[John Paul College]"; - bansi
你需要像错误信息所说的那样下载和安装驱动程序。这基本上解释了如何解决你的问题。 - Doon
很不幸,我没有升级服务器的选项。我只能使用2005版。如果我正在使用错误的驱动程序,有人知道我应该使用哪些驱动程序吗? - illusion466
请查看我上面的评论。您不能使用sqlsrv_*。尽管mssql已经被严重弃用,但仍然可用,就像SQL Server 2005一样。 - bansi
显示剩余2条评论
2个回答

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/zh/function.sqlsrv-connect.php


1

根据错误信息建议,下载SQL Native Client。这是与SQL Server分开的,并且需要此驱动程序正常工作。在IIS环境中构建sqlsrv实现时学到的教训。


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