0

因此,我一直在开发一个小型系统,其中包含用于用户兑换某种奖励的代码。

到目前为止,我在 PHP 和 HTML 中有这个脚本:http: //pastebin.com/UUSEKpev

它只显示您可以在此处看到的一个结果,我希望它在一个向下显示所有结果的表格中显示多个结果。

<?php

$yn;
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("mcv") or die(mysql_error());

$result = mysql_query("SELECT * FROM Codes")
or die(mysql_error());  

$row = mysql_fetch_array( $result );
// Print out the contents of the entry 

if ($row['Used'] == 1) {
    $yn = "Yes";
}
else{
    $yn = "No";
}

?>

<html>
<head>
    <title>Minecraft Codes</title>
    <style type="text/css">

    tbody {
        display: table-row-group;
        vertical-align: middle;
        border-color: inherit;
    }

    tr {
        display: table-row;
        vertical-align: inherit;
        border-color: inherit;
    }

    th {
        border: 1px solid #C3C3C3;
        padding: 3px;
        vertical-align: top;
        width: 20%;
        background-color: #E5EECC;
    }

    table.reference {
        background-color: white;
        border: 1px solid #C3C3C3;
        border-collapse: collapse;
        width: 50%;
    }

    table.reference td {
        border: 1px solid #C3C3C3;
        padding: 3px;
        vertical-align: top;
        width: 20%;
    }

    table, th, td, input, textarea {
        font-size: 100%;
    }

    body, p, h1, h2, h3, h4, table, td, th, ul, ol, textarea, input {
        font-family: verdana,helvetica,arial,sans-serif;
    }

    </style>
</head>

<body>
    <center>
        <br />
        <h1><u>Minecraft Server VIP Codes</u></h1>
        <br />

        <table class="reference">
            <tr>
                <th>Code</th>
              <th>Used?</th>
          </tr>
          <?php echo "<tr><td>".$row['Code']."</td><td>".$yn."</td></tr>"; ?>
        </table>
    </center>
</body>
</html>
4

3 回答 3

1

您的问题是您只获取一行:

$row = mysql_fetch_array( $result );

该行获取结果集的当前行。您想循环执行此操作:

$rows = array();
while ($row = mysql_fetch_array($result)) {
    $rows[] = $row;
}

如果可以请避免使用 mysql_ 函数,它们已被弃用,请参阅 此处的巨大警告并阅读此内容

于 2012-10-21T18:56:32.057 回答
1
<table class="reference">
    <tr>
        <th>Code</th>
        <th>Used?</th>
    </tr>

    <?php while ($row = mysql_fetch_array($result)): ?>
    <?php 
        if ($row['Used'] == 1) {
            $yn = "Yes";
        }
        else{
            $yn = "No";
        }
    ?>
    <tr>
        <td><?php echo $row['Code']; ?></td>
        <td><?php echo $yn; ?></td>
    </tr>
    <?php endwhile; ?>
</table>
于 2012-10-21T19:00:08.857 回答
0

是的,你只调用一次 mysql_fetch_row() ,因此你只检索一行。您必须将其包装到一个循环中,该循环执行的次数与行数一样多。

互联网上有数以百万计的示例,您可以从中学习...

于 2012-10-21T18:55:47.163 回答