$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;
謝謝兩位大哥