duanci9305
duanci9305
2016-06-01 15:25

通过PHP连接到映射的SQL服务器

已采纳

I am working with an already built database on 2008 Server R2 platform, and am wanting to create an application to query said DB.

All of my experience is with local installs of MySQL, however this database is an MS SQL server 10.50.

My question is this, The SQL server is a mapped drive to the web server, I've got all proper PHP drivers and SQLsrv extensions installed and am getting proper phpinfo read outs, everything is functioning properly, but I am stumped as to how to connect to the SQL server. To clarify,

My application lives on the web server, I am wanting to connect via php to the SQL server which is mapped to the web server. I'm using windows authentication.

The connection PHP i have written so far looks like this

<?php
$serverName = "SmartCM.IMG"; 


$connectionInfo = array( "Database"=>"SMARTCM");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "I tried, but it didn't work out... sorry.<br /><br><br><br>";
     die( print_r( sqlsrv_errors(), true));
}
?>

This is the error read out I get

Array ( 
    [0] => Array ( 
        [0] => 08001 
        [SQLSTATE] => 08001 
        [1] => 53 
        [code] => 53 
        [2] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. 
        [message] => [Microsoft][ODBC Driver 11 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53]. ) 
    [1] => Array ( 
        [0] => HYT00 
        [SQLSTATE] => HYT00 
        [1] => 0 
        [code] => 0 
        [2] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired 
        [message] => [Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired ) 
    [2] => Array ( 
        [0] => 08001 
        [SQLSTATE] => 08001 
        [1] => 53 
        [code] => 53 
        [2] => [Microsoft][ODBC Driver 11 for SQL Server]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][ODBC Driver 11 for SQL Server]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. ) 
    )
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqzg62440 dqzg62440 5年前

    Named Pipes Provider: Could not open a connection to SQL Server

    You need to enable Named Pipes:

    Open SQL Configuration Manager -> SQL Server Network Configuration -> Protocols -> Named Pipes -> Right-click -> Restart

    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.

    Use proper server and instance names (you need to determine both) for example PC\SQLEXPRESS.

    Also check if TCP/IP connection is enabled:

    Open SQL Configuration Manager -> SQL Server Network Configuration -> Protocols -> TCP/IP -> Right-click -> Restart

    Check if firewall is blocking a connection on port 1433.

    点赞 评论 复制链接分享