如何将mySQL数据库打印到div表中?

时间:2018-03-29 03:11:17

标签: php html

成绩表是这样的:

username  assignment  weight  mark
a         a1          10%     50%
b         a1          10%     60%

如何使用php / html打印出来?

           <div class="a">
                <div class="divTableHeading">
                    <div class="divTableRow">
                        <div class="divTableHead">Username</div>
                        <div class="divTableHead">assignment</div>
                        <div class="divTableHead">weight</div>
                        <div class="divTableHead">mark</div>
                    </div>
                </div>
                <div class="divTableBody">

            <?php
                include("config.php");
                $sql = "SELECT username, assignment, weight, mark FROM grades";
                $result = mysqli_query($db,$sql);
                while($row = $result->fetch_assoc()) {



            ?>


                    <div class="divTableRow">
                        <div class="divTableCell">$row['username']</div>
                        <div class="divTableCell">$row['assignment']</div>
                        <div class="divTableCell">$row['weight']</div>
                        <div class="divTableCell">$row['mark']</div>
                    </div>

                </div>
            </div>

$ db是来自config.php的连接数据库。我如何继续循环并输出成绩表?

意外编辑

2 个答案:

答案 0 :(得分:1)

你错过了回应你的记录值。

$row['username']更改为<?php echo $row['username']; ?>

你错过}来关闭你的while循环。使用 - <?php } ?>

关闭它

更新代码:

 <div class="divTable blueTable">
    <div class="divTableHeading">
        <div class="divTableRow">
            <div class="divTableHead">Username</div>
            <div class="divTableHead">assignment</div>
            <div class="divTableHead">weight</div>
            <div class="divTableHead">mark</div>
        </div>
    </div>
    <div class="divTableBody">

<?php
    include("config.php");
    $sql = "SELECT username, assignment, weight, mark FROM grades";
    $result = mysqli_query($db,$sql);
    while($row = $result->fetch_assoc()) {
?>
        <div class="divTableRow">
            <div class="divTableCell"><?php echo $row['username']; ?></div>
            <div class="divTableCell"><?php echo $row['assignment']; ?>/div>
            <div class="divTableCell"><?php echo $row['weight']; ?></div>
            <div class="divTableCell"><?php echo $row['mark']; ?></div>
        </div>

<?php } ?>
    </div>
</div>

答案 1 :(得分:0)

不要忘记输出你的价值

<?php echo $row['username']; ?>