将远程SQL Server连接到PHP

时间:2012-11-25 02:33:21

标签: php sql-server

任何人都可以通过以下参数解释连接字符串:

server IP : 192.168.137.4
Windows Authentication : Windows Authentication
UserName : DELL-M102Z\dell
Database : DataProd 
Network Protocol : <default>
Product Name : Microsoft SQL Server Express Edition
Server Name : DELL-M102Z\SQLEXPRESS
Instance Name : SQLEXPRESS
Computer Name : DELL-M102Z

我试过了:

$serverName = "DELL-M102Z\SQLEXPRESS"; //serverName\instanceName
$username = "DELL-M102Z\dell"; //serverName\instanceName
$conn = mssql_connect( $serverName,$username,'');

...但我得到的结果是:

  

警告:mssql_connect()[function.mssql-connect]:无法连接到服务器:第17行的C:... \ index.php中的DELL-M102Z \ SQLEXPRESS       无法建立连接。

有谁能告诉我这里有什么问题?

2 个答案:

答案 0 :(得分:2)

尝试按照以下步骤操作: http://michaelellerbeck.com/2010/03/31/cant-connect-remotely-to-sql-server-2008/ 我知道我错过了激活sql浏览器服务,这就是我无法通过远程IP连接的原因,

我从那个网站得到了结论,我必须确保:

1. make sure you have allow network connection from sql server configuration tool
2. allow connection for this port in firewall
3. activate sql browser service
4. make sure port is listen as the the service provide

我遇到了问题3,我坚持下去,这个网站解决了这个问题: http://www.wikihow.com/Enable-Remote-Connections-SQL-2008-Express

答案 1 :(得分:1)

<?php
$myServer = 'xxx.xxx.xxx.xxx:yyyy';
$myUser = 'sa';
$myPass = 'xxxxx';

$con = mssql_connect($myServer, $myUser, $myPass) or die("Could not connect to database: ".mssql_get_last_message()); 
if($con){
echo "connected";
}
// Select a database:
mssql_select_db('new') 
    or die('Could not select a database.');

// Example query: (TOP 10 equal LIMIT 0,10 in MySQL)
$SQL = "SELECT TOP 10 * FROM Table";

// Execute query:
$result = mssql_query($SQL) 
    or die('A error occured: ' . mysql_error());

// Get result count:
$count = mssql_num_rows($result);
print "Showing $count rows:<hr/>\n\n";

// Fetch rows:
while ($Row = mssql_fetch_assoc($result)) {

    print $Row['BillNo'] . "\n";

}

mssql_close($con);

$ myServer ='IP_Address:PORT';

这里分号需要ip和port 和 Mssql已下载到您的cpanel