通过 sqlsrv_connect() 连接到 mssql 数据库 - PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27308120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
connect to mssql database via sqlsrv_connect() - PHP
提问by illusion466
I'm attempting to connect to a 2005 Microsoft sql database using PHP by using the sqlsrv_connect()
function. alas, the function returns false, and im not sure why.
我正在尝试通过使用该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 );
?>
Does anyone know why I can't connect? Thanks.
有谁知道为什么我无法连接?谢谢。
Edit.
编辑。
Ok, well, i was able to bring up an error message thanks to the other guys answer, which states
好吧,感谢其他人的回答,我能够提出错误消息,其中指出
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 ) )
I'm not entirely sure how to go about fixing this issue. I'm using XAMPP as my test webserver, php version 5.3 and the following .dlls
php_sqlsrv_53_ts_vc6.dll
and
php_pdo_sqlsrv_53_ts_vc6.dll
我不完全确定如何解决这个问题。我使用 XAMPP 作为我的测试网络服务器,php 版本 5.3 和以下 .dlls
php_sqlsrv_53_ts_vc6.dll
和
php_pdo_sqlsrv_53_ts_vc6.dll
采纳答案by Janzell Jurilla
I would suggest that you display the connection error using sqlsrv_errors(). Please see the following implementation.
我建议您使用 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));
}
?>
For more info: http://php.net/manual/en/function.sqlsrv-connect.php
回答by Chillikebab
Download the SQL Native Client as the error message suggests. This is separate from SQL Server and a requirement for this driver to work correctly. Lesson learned from building an implementation of sqlsrv in an IIS environment
按照错误消息的提示下载 SQL Native Client。这与 SQL Server 是分开的,并且需要此驱动程序才能正常工作。在 IIS 环境中构建 sqlsrv 实现的经验教训