2010-07-13 181 views
1

我努力从我的PHP文件连接到我的MS SQL Express 2008版本,但它似乎几乎不可能。PHP无法连接到MS SQL Express 2008

我发现了一些关于解决问题的intarweb的指南/笔记,但是这些都没有帮助我更进一步。

我从其他网站采取了一个脚本,并进入我的数据库中的信息,但我仍然得到错误:

<?php 
$myServer = "localhost"; 
$myUser = "demo_basic"; 
$myPass = "1234"; 
$myDB = "demo_basic"; 

//connection to the database 
$dbhandle = mssql_connect($myServer, $myUser, $myPass) 
    or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with 
$selected = mssql_select_db($myDB, $dbhandle) 
    or die("Couldn't open database $myDB"); 

//declare the SQL statement that will query the database 
$query = "SELECT nitid, nisname "; 
$query .= "FROM navitems"; 

//execute the SQL query and return records 
$result = mssql_query($query); 

$numRows = mssql_num_rows($result); 
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 

//display the results 
while($row = mssql_fetch_array($result)) 
{ 
    echo "<li>" . $row["nitid"] . $row["nisname"] . "</li>"; 
} 
//close the connection 
mssql_close($dbhandle); 
?> 

我得到这个错误:

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: localhost in C:\inetpub\vhosts\dexterholding.dk\httpdocs_rip\sql.php on line 8 Couldn't connect to SQL Server on localhost

您可以看到自己:http://www.dehold.net/_rip/sql.php

任何想法? 我使用PLESK和PHP5作为FastCGI运行Windows Server 2008。

回答

0

可能是因为用户帐户没有访问SQL Server的权限。

尽管看着你的代码,你正在使用SQL身份验证,而不是Windows身份验证,大概这个帐户是在SQL Server中设置的,它被配置为允许SQL身份验证?

+0

我很习惯通过ASP连接到具有相同证书的SQL服务器,所以除非SQL服务器需要额外的设置,如果通过PHP连接,它似乎是PHP的问题。 : - / – kasperwf 2010-07-13 10:33:42

2

随着MSSQL,您的服务器名称看起来就像这样:machinenameoraddress\serverinstancename 和例子是192.168.14.201\MSSQLEXPRESSTESTMACHINE\MYTESTDBSERVER