我正在努力在我的php应用程序中连接到服务器。 服务器正在运行,我通过excel中的ODBC连接连接到它:
DSN = vortest; UID = ramunasc; Trusted_Connection = Yes; APP = Microsoft Office 2010; WSID = OFFICE22; DATABASE = vordata_sql; ApplicationIntent = READONLY;
我可以使用SQL Server Management Studio连接到服务器,我使用Windows身份验证进行连接。但PHP代码不起作用:
$serverName = "MP-SQL2\SQL2008";
$connectionInfo = array( "Database"=>"vordata_sql", "UID"=>"ramunasc");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
这会出错:
Unable to connect
Array
(
[0] => Array
(
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'ramunasc'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'ramunasc'.
)
[1] => Array
(
[0] => 28000
[SQLSTATE] => 28000
[1] => 18456
[code] => 18456
[2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'ramunasc'.
[message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'ramunasc'.
)
)
我是新的Microsoft数据库,所以我不确定我是做错了什么或误解了这些是如何工作的。
答案 0 :(得分:1)
<强>解决方案:强>
测试环境:
使用Windows身份验证连接(工作示例):
<?php
$server = '127.0.0.1';
$cinfo = array(
"Database"=>'master'
);
$conn = sqlsrv_connect($server, $cinfo);
if( $conn === false )
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
$sql = "SELECT CONVERT(varchar(32), SUSER_SNAME())";
$stmt = sqlsrv_query($conn, $sql);
if( $stmt === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
$row = sqlsrv_fetch_array($stmt);
echo "Login name: ".$row[0];
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
在我的情况下,结果是“登录名:NT AUTHORITY \ SYSTEM”。
使用SQL Server身份验证连接(工作示例):
<?php
$server = '127.0.0.1';
$cinfo = array(
"Database"=>'master',
"UID"=>'username',
"PWD"=>'password'
);
$conn = sqlsrv_connect($server, $cinfo);
if( $conn === false )
{
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
$sql = "SELECT CONVERT(varchar(32), SUSER_SNAME())";
$stmt = sqlsrv_query($conn, $sql);
if( $stmt === false ) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
$row = sqlsrv_fetch_array($stmt);
echo "Login name: ".$row[0];
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
结果是“登录名:用户名”