简体   繁体   English

我需要用 PHP 和 MYSQL 填充 HTML 表

[英]I need to populate an HTML table with PHP and MYSQL

I am making a leaderboards for a rock paper scissors game that I have made.我正在为我制作的石头剪刀布游戏制作排行榜。 I need to retrieve the data from the SQL table and insert it into my HTML table.我需要从 SQL 表中检索数据并将其插入到我的 HTML 表中。 I have got the code to retrieve the values from the table but it prints on the top left of the page and not in my CSS-Styled table.我已经获得了从表中检索值的代码,但它打印在页面的左上角而不是我的 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>

try the following:尝试以下操作:

<?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>

This will make sense if you interpret it sequentially.如果您按顺序解释它,这将是有意义的。 The query gets executed at the top : mysql_query("SELECT * FROM highscore ORDER BY Score");查询在顶部执行: mysql_query("SELECT * FROM highscore ORDER BY Score"); . . Then the HTML start to get printed out.然后 HTML 开始打印出来。 Then it runs into the while loop.然后它运行到while循环中。

Also, you should consider learning about PDO and mysqli_ and move away from mysql_ commands.此外,您应该考虑学习 PDO 和 mysqli_ 并远离 mysql_ 命令。

To address your immediate question, you are echoing the table rows before you start the HTML document or the table in which they go.为了解决您的直接问题,您在开始 HTML 文档或表格所在的表格之前回显表格行。 So the PHP prints the table info before anything else.所以 PHP 先打印表信息。 A much bigger problem is that you have a second PHP opening tag filled with non-PHP code.一个更大的问题是您有第二个 PHP 开始标记填充了非 PHP 代码。 You're also using the MySQL functions instead of MySQLi or PDO, which is a bad practice.您还使用 MySQL 函数而不是 MySQLi 或 PDO,这是一种不好的做法。

Here's a quick example to give you a better idea of what you should be doing to get the PHP variables in the HTML table.下面是一个快速示例,可让您更好地了解应该如何获取 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