0

我有以下代码

<?php

$connect=mysql_connect("localhost","root","");
mysql_select_db("dbms_teacher",$connect);
$result=mysql_query("SELECT * FROM staffs");
while ($row=mysql_fetch_array($result))
{
echo "<tr>";
    echo "<td>" . $row['fname'] . "</td>";
    echo "<td>" . $row['lname'] . "</td>";
    echo "<td>" .$row['post']. "</td>";
    echo "<td>". $row['status']. "</td>";

    // echo "<td>". "edit/delete". "</td>";
}
mysql_close($connect);

?>

这是从 dbms_teacher 调出数据的代码部分。有没有一种方法可以让我以表格形式获取数据,其中包含字段名称、帖子、状态、删除选项(我可以稍后管理)?我只需要一种以有组织的格式显示它的好方法!

4

3 回答 3

2

由于您只使用选择列,因此您可以对其进行硬编码。如果您在查询中仅获取这些列,它也会有所帮助:

$result=mysql_query("SELECT fname,lname,post,status FROM staffs");
echo "<table>";
echo "<tr><td>First Name</td><td>Last Name</td><td>Post</td><td>Status</td><td>Edit/Delete</td></tr>";
while ($row=mysql_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['fname'] . "</td>";
    echo "<td>" . $row['lname'] . "</td>";
    echo "<td>" .$row['post']. "</td>";
    echo "<td>". $row['status']. "</td>";
    echo "<td></td>";
    // echo "<td>". "edit/delete". "</td>";
    echo "</tr>";
}
mysql_close($connect);
echo "</table>";
?>
于 2013-08-14T20:17:09.700 回答
1

您必须echo "<table>";在循环之前和循环echo "</table>";之后放置。

编辑:为表格添加一些边框,并在您的循环之前手动列出列名:

echo "<table border=1>";
echo "<tr>";
echo "<td><b>fname</b></td><td><b>lname</b></td><td><b>post</b></td><td><b>status</b></td>";
echo "</tr>";
于 2013-08-14T20:06:04.100 回答
0

您需要像这样包含表结构的标记:请参阅 while 循环之外的标记:

<?php

$connect=mysql_connect("localhost","root","");
mysql_select_db("dbms_teacher",$connect);
$result=mysql_query("SELECT * FROM staffs");
echo "<table>";
while ($row=mysql_fetch_array($result))
{
echo "<tr>";
    echo "<td>" . $row['fname'] . "</td>";
    echo "<td>" . $row['lname'] . "</td>";
    echo "<td>" .$row['post']. "</td>";
    echo "<td>". $row['status']. "</td>";

    // echo "<td>". "edit/delete". "</td>";
}
echo "</table>";
mysql_close($connect);

?>
于 2013-08-14T20:09:45.420 回答