【发布时间】:2018-06-02 05:58:26
【问题描述】:
我正在尝试将 MySQL 数据库中的数据整齐地组织到表中。我快到了,但在使用 while 循环时遇到了一些问题。
发布我的简单代码:
<link type="text/css" rel="stylesheet" href="style.css"/>
<?PHP
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
WHILE($rows = mysql_fetch_array($query)):
$username = $rows['username'];
$password = $rows['password'];
$email = $rows['email'];
echo "
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead>
<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>
这给了我这样的输出:
Username Password Email Adress
test 123 test@test.no
Username Password Email Adress
testies 123 testies@test.no
Username Password Email Adress
tested 12345 tested@test.no
这里明显的问题是我的标题是在 while 循环中打印的,因此我会为每个条目获得新的标题。不过这看起来真的很愚蠢,我希望标题只出现一次。关于如何做到这一点的任何想法?
我尝试在 while 循环之前使用标题开始表格,但后来的 <td>s 不会与标题宽度对应,因为程序不会将它们识别为同一个表格,因此不需要匹配。
【问题讨论】:
-
取出循环外的头部代码。
-
循环外的标题如何与结果不对应?它们是硬编码的,不依赖于您的结果集。
-
这是非常基本的,所以我建议你多阅读一些关于一般编程的初学者教程。
-
是的,我意识到这是非常基本的,如果它不适合该网站,请见谅。无论如何,感谢您的参与,我一定要去阅读更多内容:)
标签: php mysql html-table