【发布时间】:2013-06-06 13:14:01
【问题描述】:
我们有一个在 Windows Server 2008 R2、SQL Server 2008 R2 上运行的数据库。我需要通过 PHP 查询数据库(我也可以使用其他选项)并在网页中显示数据。
我使用 UDL 测试文件测试了我的用户名、密码和服务器名称的连接性。我的连接成功了。
我的网络服务器已安装并运行 PHP 5.4。
我已经构建了一个应该连接和查询数据库的 PHP 脚本,但出现了服务器错误。
<?php
$myServer = "ipaddress";
$myUser = "username";
$myPass = "password";
$myDB = "dbname";
//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 fullName, email_address, pos_desc";
$query .= " FROM dbo.EMPLOYEE_VIEW ";
$query .= "WHERE grp_cde='STAFF'";
//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["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
请记住,数据库服务器和 Web 服务器都运行 Windows Server 2008 R2 并且存在于同一个域中。
我收到的唯一错误是: 500内部服务器错误。 您要查找的资源有问题,无法显示。
我可以做些什么来解决为什么这不起作用?
感谢您对这个问题的任何帮助。
【问题讨论】:
-
分享你的错误,这样更容易回复。
-
我收到的唯一错误是:500 - 内部服务器错误。您要查找的资源有问题,无法显示。
-
您是否使用正确的值更改了前 4 行中的服务器参数? ipaddress 应该是 localhost 或 127.0.0.1,类似地,用户名和密码应该是您的数据库服务器中使用的一个。只是询问您是否确实更改了它们。
-
是的,我使用测试连接成功的实用程序测试了我的服务器名称、用户名和密码。结果成功返回。虽然由于 msSQL 服务器在不同的服务器上运行,但我使用的是该服务器的 IP 地址而不是 localhost。
-
我认为这可能与 PHP 配置或 msSQL 服务器配置有关。哦,顺便说一句,我们的旧网络服务器目前正在使用 asp.net 来查询它,它可以正常工作。
标签: php database wordpress sql-server-2008