【发布时间】: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.dll
和
php_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