繁体   English   中英

我需要用 PHP 和 MYSQL 填充 HTML 表

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM