迈着小碎步 發表於 2022-6-14 10:03:00

PHP连接SQLServer

<div>PHP连接SQLServer</div>
<div>连接前配置系统:</div>
<div>1.检查文件&nbsp;php5.2.5/ntwdblib.dll&nbsp;默认下面有一个,不能连接再替换.</div>
<div>下载正确版本的&nbsp;ntwdblib.dll&nbsp;(2000.80.194.0),地址:&nbsp;http://webzila.com/dll/1/ntwdblib.zip</div>
<div>2.配置php</div>
<div>a、打开php.in将extension=php_mssql.dll的注释符号去掉。</div>
<div>b、打开php.in将mssql.secure_connection&nbsp;=&nbsp;Off改为on。</div>
<div>c、将php_mssql.dll拷贝到php.in中extension_dir&nbsp;指定的目录或者系统system32目录下。(php_mssql.dll在php的压缩安装包中有)。</div>
<div>以上步骤完成后需要重启apache。</div>
<div>注意:实际使用中发现&nbsp;如果通过php压缩文件手工安装php到iis下,必须重启机器而不仅仅是iis。</div>
<div>3.配置sqlserver&nbsp;</div>
<div>a.&nbsp;运行&nbsp;SQL&nbsp;Server&nbsp;配置管理器:SQL&nbsp;Server&nbsp;Configuration&nbsp;Manager,打开协议&nbsp;Protocols&nbsp;</div>
<div>b.&nbsp;允许命名管道&nbsp;"named&nbsp;pipes"&nbsp;和&nbsp;"tcp/ip"&nbsp;</div>
<div>c.&nbsp;右键点击&nbsp;"tcp/ip",打开属性&nbsp;Properties&nbsp;标签&nbsp;"IP&nbsp;addresses"&nbsp;</div>
<div>d.&nbsp;在&nbsp;TCP&nbsp;动态端口&nbsp;"TCP&nbsp;Dynamic&nbsp;Ports"&nbsp;填入&nbsp;1433&nbsp;</div>
<div>e.&nbsp;重启&nbsp;SQL&nbsp;Server</div>
<div>-----------------------------------------------------------------------------------------------&nbsp;&nbsp;</div>
<div>4.使用以下方式连接MS&nbsp;SQL&nbsp;Server&nbsp;2005:&nbsp;</div>
<div>代码如下:</div>
<div>//链接数据库&nbsp;</div>
<div>&nbsp;$conn=mssql_connect('localhost','sa','123456');&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;&nbsp;&nbsp;mssql_select_db('gu_dde',$conn);&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>//query语句&nbsp;&nbsp;&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;$Query="select&nbsp;*&nbsp;from&nbsp;dde_top";&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;$AdminResult=mssql_query($Query);&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>//输出结果&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;$Num=mssql_num_rows($AdminResult);&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;for($i=0;$i&lt;$Num;$i++)&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;&nbsp;&nbsp;{&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;$Row=mssql_fetch_array($AdminResult);&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;echo($Row);&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;echo("</div>
<div>");&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;</div>
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="0" data-media-type="image"></div>
<div>?&gt;</div>
<div>输入http://127.0.0.1</div>
<div>-----------------------------------------------------------------------</div>
<div>5.FAQ常见问题:</div>
<div>1报错:</div>
<div>Fatal&nbsp;error:&nbsp;Call&nbsp;to&nbsp;undefined&nbsp;function&nbsp;mssql_connect()</div>
<div>解决:</div>
<div>使用MSSQL_系列函数&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;要使用这两种都需要在php.ini进行设定:&nbsp;&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;(1)允许&nbsp;DCOM,需要将php.ini中的&nbsp;;com.allow_dcom=TRUE前的分号";"去掉。&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;(2)使用MSSQL扩展,需要php.ini中的&nbsp;;extension=php_mssql.dll前的分号";"去掉。(关键)&nbsp;&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;(3)确认extension_dir为正确路径,以本机为例:extension_dir&nbsp;=&nbsp;"c:/AppServ5.2.6/php/ext"。&nbsp;&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;(4)如果仍然机器报错说找不到c:/AppServ5.2.6/php/ext/php_mssql.dll但明明存在这个文件。&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;解决方法:将php_mssql.dll,ntwdblib.dll拷贝到系统目录/system32下重启测试。。&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;(注:上面两个dll文件不在相同目录下,我的为c:/AppServ5.2.6/php/ext/php_mssql.dll;c:/AppServ5.2.6/php/ntwdblib.dll)&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;另外设置好了后记得重启服务器哦。</div>
<div>2.mssql_connect()&nbsp;Unable&nbsp;to&nbsp;connect&nbsp;to&nbsp;server</div>
<div>确认SQLServer2005服务器正常.检查&nbsp;TCP/IP已经启用</div>
<div>同时右键查看属性:</div>
<div>已经启用是否选择是</div>
<div>确认服务器正确之后,再确认ntwdblib.dll&nbsp;文件位置是否放到了&nbsp;c:/windows/system32下</div>
<div>同时要保证ntwdblib.dll&nbsp;这个文件的版本和sqlserver的版本对应:</div>
<div>下面是对应关系:</div>
<div>2.ntwdblib.dll&nbsp;版本为&nbsp;2000.2.8.0&nbsp;是&nbsp;对应&nbsp;SqlServer2000(这个是网络查资料和猜测,没装2000)</div>
<div>2.ntwdblib.dll&nbsp;版本为&nbsp;2000.80.194.0&nbsp;是&nbsp;对应&nbsp;SqlServer2005(这个是用实验证明可以用,本人就是用笔记本装了2005)</div>
<div>3.ntwdblib.dll&nbsp;版本为&nbsp;2000.80.2039&nbsp;是&nbsp;对应&nbsp;SqlServer2008(这个是猜测没有装2008)</div>
<div>&nbsp;</div>
<div>6.其他问题:</div>
<div>如果php&nbsp;apache&nbsp;Sql&nbsp;Server2000都在同一台机器上,访问基本没有问题了。</div>
<div>如&nbsp;果Sql&nbsp;Server2000和php机器是分离的,需要确认ping&nbsp;sqlserver所在机器的机器名能通,如过不通,修改php所在机器的/system32/drivers/etc下的hosts文件,增加一行&nbsp;sqlserver所在机器的机器ip&nbsp;&nbsp;&nbsp;sqlserver所在机器的机器名字。</div>
<div>如果还是无法访问,需要确认php所在的机器有无暗转mdac。要不索性安装一下sqlserver的客户端好了。</div>
<div>解决问题如下:</div>
<div>1.下载两个文件&nbsp;php_mssql.dll&nbsp;和&nbsp;ntwdblib.dll</div>
<div>php_mssql.dll&nbsp;如果这个没有复制到c:/windows/system32下,就很容易出现</div>
<div>&nbsp;</div>
<div>ntwdblib2093.dll&nbsp;这个文件要注意版本,不然后面搞得很郁闷.</div>
<div>&nbsp;</div>
<div>64位系统要复制到&nbsp;C:\Windows\SysWOW64?</div>
<div>&nbsp;</div>
<div>pHP链接数据库报错Warning:&nbsp;mssql_connect()&nbsp;:&nbsp;message:&nbsp;用户&nbsp;'NT&nbsp;AUTHORITY\IU</div>
<div>、php.ini有个mssql的设置&nbsp;mssql.secure_connection&nbsp;改成&nbsp;off。(我就因为这个弄了好久,ON的意思是使用安全连接,实际上是使用nt账户登录,并非使用mssql账户登录。一般我们远程连接都是使用mssql账户登录)</div>

</div>
<div id="MySignature" role="contentinfo">
    专业从事基于C#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP,CRM,企业进销存等各种数据库管理系统开发。Asp.net,Asp.net mvc,Webservice,WCF, Webapi等服务程序开发。
基于Oracle MySQL MSSql postgresql各种数据库的管理系统数据同步服务。以及基于MapXtreme, Arcgis Engine ,以及基于Arcgis for silverlight/Javascript的WebGIS等相关的GIS系统二次开发。基于Windows 10 Mobile的移动端开发方案。针对各种系统的二次开发维护,并提供相关开发的技术性支持,如程序BUG解决,应用系统架构,技术难题攻克等相关技术服务。
联系方式: QQ :80163278(devgis) 邮箱:devgis@qq.com<br><br>
来源:https://www.cnblogs.com/devgis/p/16373527.html
頁: [1]
查看完整版本: PHP连接SQLServer