PHP串連sqlserver2008,怎麼串連指定ip地址上的資料庫

來源:互聯網
上載者:User
$serverName = "(local)"; //資料庫伺服器地址$uid = "sa"; //資料庫使用者名稱$pwd = "password123"; //資料庫密碼$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"test");$conn = sqlsrv_connect( $serverName, $connectionInfo);if( $conn == false){    echo "串連失敗!";    die( print_r( sqlsrv_errors(), true));}if($conn){echo "串連成功";}

串連成功了,但是怎麼把local改成127.0.0.1這樣的


回複討論(解決方案)

直接換成ip地址

$serverName = "ip地址";
或
$serverName = "電腦名稱";
如果伺服器上有多個執行個體,還需要有執行個體名

遠端連線還需開啟遠端連線服務
由於 sqlserver 2000 的預設遠端連線,導致了 SQL攻擊 的盛行。
所以現在都學乖了,不經繁瑣的遠端連線授權,是無法串連的

header("Content-type: text/html; charset=gb2312");$serverName = "127.0.0.1/MSSQLSERVER"; //資料庫伺服器地址$uid = "sa"; //資料庫使用者名稱$pwd = "password123"; //資料庫密碼$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"test");$conn = sqlsrv_connect( $serverName, $connectionInfo);//$conn= new PDO("sqlsrv:Server=127.0.0.1;Database=test","sa","yyyy1111") or die ("PDO Connection faild.");if( $conn == false){    echo "串連失敗!";    die( print_r( sqlsrv_errors(), true));}if($conn){echo "串連成功";}

結果:
串連失敗!Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 67 [code] => 67 [2] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [67]. [message] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [67]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 67 [code] => 67 [2] => [Microsoft][SQL Server Native Client 11.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 11.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. ) )

header("Content-type: text/html; charset=gb2312");$serverName = "127.0.0.1"; //資料庫伺服器地址$uid = "sa"; //資料庫使用者名稱$pwd = "password123"; //資料庫密碼$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"test");$conn = sqlsrv_connect( $serverName, $connectionInfo);//$conn= new PDO("sqlsrv:Server=127.0.0.1;Database=test","sa","yyyy1111") or die ("PDO Connection faild.");if( $conn == false){    echo "串連失敗!";    die( print_r( sqlsrv_errors(), true));}if($conn){echo "串連成功";}

結果:串連失敗!Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 5 [code] => 5 [2] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [5]. [message] => [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [5]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired [message] => [Microsoft][SQL Server Native Client 11.0]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 5 [code] => 5 [2] => [Microsoft][SQL Server Native Client 11.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 11.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. ) )

1、運行 SQL Server 組態管理員:SQL Server Configuration Manager,開啟協議 Protocols
允許具名管道 "named pipes" 和 "tcp/ip"
2、 右鍵點擊 "tcp/ip",開啟屬性 Properties 標籤 "IP addresses"
3、 在 TCP 動態連接埠 "TCP Dynamic Ports" 填入 1433
4、 重啟 SQL Server

伺服器執行個體這樣寫:
$serverName = "127.0.01\sqlexpress, 1542";1542可以不寫,預設是1433;

謝謝兩位大哥

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.