【问题标题】:I need to populate an HTML table with PHP and MYSQL我需要用 PHP 和 MYSQL 填充 HTML 表
【发布时间】:2014-04-09 19:11:13
【问题描述】:

我正在为我制作的石头剪刀布游戏制作排行榜。我需要从 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>

【问题讨论】:

  • 你模糊了 html 和 php 之间的界限,&lt;?php 不应该包含 html
  • 我修复了这个问题,但它仍然没有填充我表中的行
  • 不要使用mysql_*。它已被弃用。

标签: php html mysql sql


【解决方案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_ 命令。

【讨论】:

  • 解释你所做的可能会有所帮助
【解决方案2】:

为了解决您的直接问题,您在开始 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>

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    相关资源
    最近更新 更多