所以我有一个数据库,我希望以表格的形式输出数据。
<table>
<tr>
<th class="tg-031e">Username</th>
<th class="tg-031e">Password</th>
</tr>
..
</table>
现在我得到这样的数据:
<?php include 'connect.php';
echo "<tr><td class='tg-031e'>";
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
while($row = mysql_fetch_array($exec)){
echo $row['Username'] . "</td>";
} ?>
然而,在这种情况下,我需要独立地回显每列。当MySQL列中出现新数据时,如何使PHP动态创建包含数据库信息的HTML表行?
答案 0 :(得分:1)
<?php include 'connect.php';
$SQL = "SELECT `Username`, Password FROM `Users`";
$exec = mysql_query($SQL, $connection);
echo "<table><tr><td class="tg-031e">Username</td><td class="tg-031e">Password</td></tr>";
while($row = mysql_fetch_array($exec))
{
echo "<tr><td class='tg-031e'>";
echo $row['Username'] . "</td>";
echo "<td class='tg-031e'>".$row['Password'] . "</td></tr>";
}
echo "</table>";
?>
答案 1 :(得分:1)
像这样修改你的PHP代码:
<?php include 'connect.php';
$SQL = "SELECT `Username` FROM `Users` WHERE ID=1";
$exec = mysql_query($SQL, $connection);
echo "<table>";
echo "<tr>";
echo "<th class="tg-031e">Username</th>";
echo "<th class="tg-031e">Password</th>";
echo "</tr>";
while($row = mysql_fetch_array($exec)){
//add as many fields in record, i.e: username, password... etc.
echo "<tr>";
echo "<td class='tg-031e'>" . $row['Username'] . "</td>";
echo "<td class='tg-031e'>" . $row['Password'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>