50l*_*l3r 2 php sql-server apache sql-server-2008
我在将 Windows Apache 连接到 SQL Server Express 2008 时遇到问题
PHP 版本 5.2.9
Apache 版本 Apache/2.2.11 (Win32)
<?php
$serverName = "serverName\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$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));
}
?>
Run Code Online (Sandbox Code Playgroud)
我尝试使用 mssql 和 sqlsrv 驱动程序,但无法连接到数据库。这些驱动程序已正确启用。

我可以连接 sqlcmd 应用程序并成功执行查询:
sqlcmd -S serverName\SQLEXPRESS-U userName-P password
Run Code Online (Sandbox Code Playgroud)
当我尝试连接 mssql 驱动程序时:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: serverName\SQLEXPRESS in C:\xampp\htdocs\ci\demo.php on line 12
Couldn’t connect to SQL Server on $myServer
When i try to connect with sqlsrv driver:
Connection could not be established.
Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] => [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. [message] => [Microsoft][SQL Server Native Client 10.0]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 10.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 10.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => -1 [code] => -1 [2] => [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,我使用此代码并连接了它。数据库服务器适用于我的情况 10.105.88.99 在另一台机器上
//i use sql server 2012
$serverName = '10.105.88.99';//only the server name
$connectionInfo = array( "Database"=>"mydbName", "UID"=>"myUserId", "PWD"=>"myPass");
$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));
}
Run Code Online (Sandbox Code Playgroud)