使用php连接的SQL Server地址 [英] SQL Server address to connect using php

查看:28
本文介绍了使用php连接的SQL Server地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码连接到 SQL Server 2012,但它不起作用

I use this code to connect to SQL Server 2012, but it does not work

<?php
$objConnect = mssql_connect("localhost","usr","pass"); 

if($objConnect)  
{  
  echo "Database Connected.<br />";  
  echo mssql_error();
}  
else  
{  
  echo "Database Connect Failed.<br />";
}  

mssql_close($objConnect);  
?>  

它总是打印Database Connect Failed.

另外,如何从外部 Web 服务器连接到本地 SQL Server?

Also how can I connect to my local SQL Server from an external web server?

推荐答案

如果您的版本是 SQL Server Express,您可能应该使用:

If your edition is SQL Server Express, you should probably be using:

$objConnect = mssql_connect("localhost\SQLEXPRESS","usr","pass");

或者如果它是一个命名实例,则

Or if it is otherwise a named instance, then

$objConnect = mssql_connect("localhost\InstanceName","usr","pass");

如果您需要远程连接,那么显然您不应该使用 localhost,因为远程 Web 服务器如何定位您的 localhost?您应该使用以下其中一种(假设远程 Web 服务器可以看到您的机器的 IP 地址为 192.168.5.22):

If you need to connect remotely, then obviously you shouldn't be using localhost since how does the remote web server locate your localhost? You should be using one of the following (assuming the remote web server can see your machine with IP address 192.168.5.22):

$objConnect = mssql_connect("192.168.5.22\SQLEXPRESS","usr","pass");
$objConnect = mssql_connect("192.168.5.22\NamedInstance","usr","pass");
$objConnect = mssql_connect("192.168.5.22","usr","pass");

当然,您的防火墙必须打开端口 1433(也可能是 1434)才能接受该连接,而且这里也可能出现各种其他问题.

Of course your firewall must have port 1433 (and possibly 1434) open in order to accept that connection, and there are a variety of other things that can go wrong here as well.

不过,有点调试101的建议.而不是:

However, a little debugging 101 suggestion. Instead of:

if($objConnect)  
{  
  echo "Database Connected.<br />";  
  echo mssql_error();
}  
else  
{  
  echo "Database Connect Failed.<br />";  
}  

为什么不:

if($objConnect)  
{
  echo "Database Connected.<br />";  
}  
else  
{  
  echo "Database Connect Failed.<br />";  
  echo mssql_error();
}  

当数据库连接成功时,您当然不需要向页面写入错误.告诉我们您收到的实际错误消息可能会更好地让我们为您指明解决方案的方向.您编写的通用数据库连接失败"消息不会向任何人提供有关实际出错的任何线索.但我敢打赌 mssql_error() 可能!

Surely you don't need to write an error to the page when the database connects successfully. And telling us the actual error message you receive may better equip us to point you in the direction of a solution. A generic "Database Connect Failed" message that you wrote is not going to give anyone any clue about what actually went wrong. But I bet mssql_error() might!

这篇关于使用php连接的SQL Server地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆