【发布时间】:2012-08-24 18:55:33
【问题描述】:
我已成功创建到 MSSQL 数据库的连接,执行查询并使用 mssql_num_rows 返回结果。我可以让$numRows 回显并在页面上显示“返回的行”。
表中的列具有以下名称:employeeName、prideSelect、prideComment、flagpoleSelect、flagpoleComment。都是 nvarchar(50) 或 nvarchar(max)
我不能做的是回显表中的任何数据。我是 php/sql 的新手,我尝试寻找没有运气的解决方案。我不确定它是否与设置为 nvarchar 的数据库列有关。或者,如果这是我的代码中的错误。我试过mssql_fetch_array 和mssql_fetch_row 来显示结果,但都无法工作。以下是我正在使用的代码:
<?php
//connects to Heros database
$conn = mssql_connect($host, $uid, $pwd) or die('Could not connect')
or die("Couldn't connect to SQL Server on $host");
$selected = mssql_select_db($db, $conn)
or die("Couldn't open database $db");
echo "MSSQL Connection successful";
//declare the SQL statement that will query the database
$query = mssql_query('SELECT * FROM [Heros].[dbo].[tblHeros]');
//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["employeeName"] . $row["prideSelect"] . $row["prideComment"] . "</li>";
}
//close the connection
mssql_close($conn);
?>
【问题讨论】:
-
var_dump($result)产生什么? -
您可能希望将这些
<li>元素放在<ul>或<ol>标记内。 -
@user1623347 - 这些是您实际的数据库登录详细信息吗?
-
@andrewsi 我删除了这些行。 SMH
-
您的
<li>行是空的,还是根本没有<li>行?空行 = 在$row中使用了错误的引用。根本没有行 = 查询没有产生任何结果。
标签: php sql-server