php sqlserver08001,PHP连接sqlserver2008,怎么连接指定ip地址上的数据库

$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;

谢谢两位大哥

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值