我正在为我制作的石头剪刀游戏制作排行榜。我需要从SQL表中检索数据并将其插入到我的HTML表中。我有代码从表中检索值,但它打印在页面的左上角而不是我的CSS-Styled表中。
<?php
$connect = mysql_connect("localhost","username", "passworkd");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("username");
$results = mysql_query("SELECT * FROM highscore ORDER BY Score");
while($row = mysql_fetch_array($results)){
?>
<tr>
<td><?php echo $row['Name']?></td>
<td><?php echo $row['Score']?></td>
<td><?php echo $row['Date']?></td>
</tr>
}
?>
<?php
<html>
<head>
<link rel="stylesheet" href ="css/table.css">
<title>Leaderboards - Rock, Paper, Scissors</title>
</head>
<body>
<div class="easyTable" >
<table >
<tr>
<td colspan="3"><a style="color: white;"href="TypingTest.html">Typing Test, WPM - Easy<a></td>
</tr>
<tr id="easyTitle">
<td style="color: white;"> <?php echo $row['Name']?>
Name
</td>
<td style="color: white;"><?php echo $row['Score']?>
Score
</td>
<td style="color: white;"><?php echo $row['Date']?>
Date
</td>
</tr>
<tr>
<td >
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td >
Row 2
</td>
<td>
Row 2
</td>
<td>
Row 2
</td>
</tr>
<tr>
<td >
Row 2
</td>
<td>
Row 2
</td>
<td>
Row 2
</td>
</tr>
<tr>
<td >
Row 3
</td>
<td>
Row 3
</td>
<td>
Row 3
</td>
</tr>
<tr>
<td >
Row 2
</td>
<td>
Row 2
</td>
<td>
Row 2
</td>
</tr>
<tr>
<td >
Row 2
</td>
<td>
Row 2
</td>
<td>
Row 2
</td>
</tr>
<tr>
<td >
Row 2
</td>
<td>
Row 2
</td>
<td>
Row 2
</td>
</tr>
<tr>
<td >
Row 2
</td>
<td>
Row 2
</td>
<td>
Row 2
</td>
</tr>
</table>
</div>
答案 0 :(得分:1)
尝试以下方法:
<?php
$connect = mysql_connect("localhost","username", "passworkd");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("username");
$results = mysql_query("SELECT * FROM highscore ORDER BY Score");
?>
<html>
<head>
<link rel="stylesheet" href ="css/table.css">
<title>Leaderboards - Rock, Paper, Scissors</title>
</head>
<body>
<div class="easyTable" >
<table >
<tr>
<td colspan="3"><a style="color: white;"href="TypingTest.html">Typing Test, WPM - Easy<a></td>
</tr>
<?php while($row = mysql_fetch_array($results)) : ?>
<tr id="easyTitle">
<td style="color: white;"> <?php echo $row['Name']?>
Name
</td>
<td style="color: white;"><?php echo $row['Score']?>
Score
</td>
<td style="color: white;"><?php echo $row['Date']?>
Date
</td>
</tr>
<?php endwhile;?>
</table>
</div>
如果按顺序解释它,这将有意义。查询在顶部执行:mysql_query("SELECT * FROM highscore ORDER BY Score");
。然后HTML开始打印出来。然后它进入while循环。
另外,你应该考虑学习PDO和mysqli_并远离mysql_命令。
答案 1 :(得分:0)
要解决您的直接问题,请在启动HTML文档或其中的表之前回显表行。所以PHP先打印表信息。一个更大的问题是你有第二个PHP开头标记填充非PHP代码。您还使用MySQL函数而不是MySQLi或PDO,这是一种不好的做法。
这是一个快速示例,可以让您更好地了解在HTML表格中获取PHP变量应该做些什么。
<?php $connect = mysql_connect("localhost","username", "passworkd");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("username");
$results = mysql_query("SELECT * FROM highscore ORDER BY Score");
?>
<!DOCTYPE html>
<html>
<head><!--header stuff--></head>
<body>
<table>
<?php while($row = mysql_fetch_array($results)): ?>
<tr>
<td><?php echo $row['Name']; ?></td>
<td><?php echo $row['Score']; ?></td>
<td><?php echo $row['Date']; ?></td>
</tr>
<?php endwhile; ?>
</table>
</body>
</html>