-2

我是 PHP 和 mysql 的新手,如果这是一个新手错误,我很抱歉。

我很难从 mysql 获取数据并将其填充到 html 表中。

所以我的脚本如下:

<html>
<head>

</head>
<body>
    <div class="well">
        <table class="table table-striped">
            <?php
            $userDetail = mysql_query("SELECT * FROM uyePopup ORDER BY id LIMIT 100") or die(mysql_error());
            $useridTable = "";
            $userGenderTable = "";
            $userMailTable = "";
            while ($userRow = mysql_fetch_array($userDetail)) {

                $useridTable.= '<td>' . $userRow['id'] . '</td>';
                if ($userRow['gender'] == 1) {
                    $userRow['gender'] = 'M';
                } else {
                    $userRow['gender'] = 'F';
                }
                $userGenderTable.= '<td>' . $userRow['gender'] . '</td>';
                $userMailTable.= '<td>' . $userRow['email'] . '</td>';
            }
            // echo $userTable;
            ?>
            <thead>
            <tr>
                <th>ID</th>
                <th>Gender</th>
                <th>E-mail</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <?= $useridTable ?>
            </tr>
            <tr>
                <?= $userGenderTable ?>
            </tr>
            <tr>
                <?= $userMailTable ?>
            </tr>
            </tbody>
        </table>
    </div>
</body>
</html>

所以我有这样的输出:

我已经用谷歌搜索了很长时间,但似乎没有任何效果。

4

1 回答 1

1

这是未经测试的,但这样的事情应该可以工作:

<html>
<head>

</head>
<body>
    <div class="well">
        <table class="table table-striped">
            <thead>
            <tr>
                <th>ID</th>
                <th>Gender</th>
                <th>E-mail</th>
            </tr>
            </thead>
            <tbody>
            <?php
            $userDetail = mysql_query("SELECT * FROM uyePopup ORDER BY id LIMIT 100") or die(mysql_error());
            while ($userRow = mysql_fetch_array($userDetail)) {
               echo '<tr><td>' . $userRow['id'] . '</td>';
               echo '<td>'.($userRow['gender'] == 1 ? 'M' : 'F').'</td>';
               echo '<td>' . $userRow['email'] . "</td></tr>\n";
            }
            ?>
            </tbody>
        </table>
    </div>
</body>
</html>
于 2013-08-21T23:35:51.907 回答