我正在尝试使用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.dll
和php_pdo_sqlsrv_53_ts_vc6.dll
John Paul College
吗?带有所有这些空格吗?那么我认为你应该使用$myDB = "[John Paul College]";
。 - bansi