简体   繁体   English

PHP连接到MS SQL Server 2008 R2不成功

[英]PHP Connection To MS SQL Server 2008 R2 Unsuccessful

Using MS SQL Server 2008 R2: 使用MS SQL Server 2008 R2:

<?PHP
    $servername = "";    //serverName\instanceName
//  $username = "";    // exclude UID, using windows authentication
//  $password = "";    // exclude PWD, using windows authentication
    $connectionInfo = array("Database"=>"db.mdf");
    $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));
    }
?>

Notice: Undefined variable: serverName in C:\\OES\\test.php on line 97 Call Stack: 0.0270 336232 1. {main}() C:\\OES\\test.php:0 Connection could not be established. 注意:未定义的变量:第97行的C:\\ OES \\ test.php中的serverName调用堆栈:0.0270 336232 1. {main}()C:\\ OES \\ test.php:0无法建立连接。

Please help me fix the connection. 请帮我修复连接。

Variable names in PHP are case sensitive PHP中的变量名称区分大小写

Variables in PHP are represented by a dollar sign followed by the name of the variable. PHP中的变量由美元符号后跟变量名称表示。 The variable name is case-sensitive. 变量名称区分大小写。 1 1

So change the variable name in either the first line or the 4th line to match the other: 因此,在第一行或第四行中更改变量名称以匹配另一行:

$servername = "serverName\instanceName";    //serverName\instanceName
$conn = sqlsrv_connect($servername , $connectionInfo);

Also the database name sent to sqlsrv_connect should not have the file name - remove the extension (ie .mdf ): 此外,发送到sqlsrv_connect的数据库名称不应具有文件名 - 删除扩展名(即.mdf ):

$connectionInfo = array("Database"=>"my_db");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM