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

php中文网
发布: 2016-06-23 13:53:34
原创
1660人浏览过

$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这样的

阿里云-虚拟数字人
阿里云-虚拟数字人

阿里云-虚拟数字人是什么? ...

阿里云-虚拟数字人 2
查看详情 阿里云-虚拟数字人

回复讨论(解决方案)

直接换成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
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号