[英]I need to populate an HTML table with PHP and MYSQL
我正在為我制作的石頭剪刀布游戲制作排行榜。 我需要從 SQL 表中檢索數據並將其插入到我的 HTML 表中。 我已經獲得了從表中檢索值的代碼,但它打印在頁面的左上角而不是我的 CSS 樣式表中。
<?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>
嘗試以下操作:
<?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_ 命令。
為了解決您的直接問題,您在開始 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.