[英]Displaying MySQL data in a table
I'm trying to neatly organize data from my MySQL database into tables. 我试图将MySQL数据库中的数据整齐地组织到表中。 I'm almost there, but experiencing some troubles with my use of a while loop.
我快到了,但是在使用while循环时遇到了一些麻烦。
Posting my simple code: 发布我的简单代码:
<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;
?>
This gives me an output of something like this: 这给了我这样的输出:
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
The obvious problem here is that my headers are printed inside the while loop, and thus I get new headers for every entry. 这里的明显问题是我的标题打印在while循环内,因此我为每个条目获取新的标题。 This looks really stupid though, and I would prefer to have the headers only appear once.
不过,这看起来确实很愚蠢,我希望标头只出现一次。 Any ideas on how to do this?
有关如何执行此操作的任何想法?
I have tried to start the table with headers before the while loop, but then the later <td>
s wont correspond with the headers width, since the program doesn't recognize them as the same table, and thus not needing to match. 我试图在while循环之前用表头启动表,但是后来的
<td>
不会与表头宽度相对应,因为程序无法将它们识别为同一表,因此不需要匹配。
Use this 用这个
<link type="text/css" rel="stylesheet" href="style.css"/>
<?PHP
$connect = mysql_connect("localhost","root","");
mysql_select_db("users"); ?>
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead><?php
$query = mysql_query("SELECT * FROM users");
WHILE($rows = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $rows['username']; ?></td>
<td><?php echo $rows['password']; ?></td>
<td><?php echo $rows['email']; ?></td>
</tr>
<?php } ?>
?>
change your code as follows. 如下更改代码。
<?PHP
$connect = mysql_connect("localhost","root","");
mysql_select_db("users");
$query = mysql_query("SELECT * FROM users");
echo "
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead>";
WHILE($rows = mysql_fetch_array($query)):
$username = $rows['username'];
$password = $rows['password'];
$email = $rows['email'];
echo"<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>
print the headers before the loop. 在循环之前打印标题。
<table>
<thead>
<tr>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</tr>
</thead>
<?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 "
<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
</tr>
";
endwhile;
?>
</table>
Try this: 尝试这个:
<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");
?>
<table>
<thead>
<th>Username</th>
<th>Password</th>
<th>Email Adress</th>
</thead>";
<?php
WHILE($rows = mysql_fetch_array($query)):
$username = $rows['username'];
$password = $rows['password'];
$email = $rows['email'];
echo "<tr>
<td>$username</td>
<td>$password</td>
<td>$email</td>
";
endwhile;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.