且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP将MySQL查询结果显示到表中

更新时间:2022-11-27 08:44:57

我认为我们都非常看重一个非常简单的问题。您已在查询中使用 SELECT * ,因此您已经从表格中提取了所有三列。所以现在,你需要做的是在表格的每一行添加另一个单元格。

I think we're all looking too hard at a VERY simple problem. You are already using SELECT * in your query, so you're already fetching all three columns from your table. So now, all you need to do is add another cell to each row of your table.

echo "<tr><td>" . htmlspecialchars($row['username']) . "</td><td>" . htmlspecialchars($row['time']) . "</td></tr>";

为了确保以正确的顺序读取行,您应该添加一个 ORDER BY 到您的查询中:

And to make sure you are fetching your rows in the correct order, you should add an ORDER BY to your query:

SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time

如果您不指定 ORDER BY 子句,你不能保证你会得到任何特定顺序的结果。

If you don't specify an ORDER BY clause, you have no guarantee that you will get the results in any particular order.

最后一件事,你循环两遍,不必要的。摆脱foreach循环,并将回声直接放在while循环中。

And one last thing, you are looping through the rows twice, unnecessarily. Get rid of the foreach loop and put the echo directly inside the while loop.