PHP不将数据库打印为表

我正在尝试将 mySQL 数据库打印到 html 表中。我看过很多关于如何做到这一点的教程,但不确定我如何在我的 php 代码中引用 html 表。信息打印得很好并连接到数据库,但由于某种原因它没有以表格格式输出。


<?php

    $conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');

    if (!$conn) {

        echo "Connection failed:" . mysqli_connect_error();

    }

    //Writing query for database.

    $sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date Created`";


    //Querying and getting results

    $result = mysqli_query($conn, $sql);


    if ($result->num_rows > 0) {

        while ($row = $result->fetch_assoc()) {

            echo "<tr><td>" . $row["First Name"] . "</td></tr>" . $row["Last Name"] . "</td></tr>"

                . $row["Emails"] . "</td></tr>" . $row["Date Created"] . "</td></tr>";

        }

        echo "</table>";

    } else {

        echo "0 result";

    }


    //Fetch resulting rows as an array

    $informed = mysqli_fetch_all($result, MYSQLI_ASSOC);


    // Freeing result from the memory.

    mysqli_free_result($result);


    mysqli_close($conn);

?>


<!DOCTYPE html>

<html>

    <head>

        <div class="Contained">

            <div class="row">

                <?php foreach ($informed as $inform) { ?>

                    <div class="col s6 medium-3">

                        <div class="card z-depth-0">

                            <div class="card-content center">

                                <h6><?php echo htmlspecialchars($inform['First Name']); ?></h6>

                                <div><?php echo htmlspecialchars($inform['Last Name']); ?></div>

                            </div>

                            <div class="card-action right-align">

                                <a class="brand-text" href="#">More Info

                            </div>

                        </div>

                    </div>

                <?php } ?>

            </div>

        </div>

        <title> Email and Name List </title>

    </head>

浏览器gyazo中的输出:


潇湘沐
浏览 133回答 2
2回答

慕标5832272

您必须更改在 php 中输出所有表的代码,例如:<body><?php$conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');if (!$conn){echo "Connection failed:" . mysqli_connect_error();}//Writing query for database.$sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date&nbsp;Created`";//Querying and getting results$result = mysqli_query($conn,$sql);if ($result->num_rows>0){echo '<table><tr>&nbsp; &nbsp; <th>First Name</th>&nbsp; &nbsp; <th>Last Name</th>&nbsp; &nbsp; <th>Emails</th>&nbsp; &nbsp; <th>Date Created</th></tr>';&nbsp; &nbsp; while($row = $result->fetch_assoc()){&nbsp; &nbsp; &nbsp; &nbsp; echo "<tr> ";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row["First Name"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row["Last Name"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" . $row["Date Created"] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; echo "</tr> ";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; echo"</table>";}else{&nbsp; &nbsp; echo "0 result";}//Fetch resulting rows as an array$informed = mysqli_fetch_all($result, MYSQLI_ASSOC);// Freeing result from the memory.mysqli_free_result($result);mysqli_close($conn);?></body>另一个问题你确定是$row["First Name"]不是$row["First_Name"]?最后提示了解如何准备 stm 以防止 sql 注入

墨色风雨

根据您的代码,您尝试在下面定义实际表之前打印该表。你可以尝试这样的事情:<?php&nbsp; &nbsp; $conn = mysqli_connect('localhost', 'Admin', 'admin1', 'info');&nbsp; &nbsp; if (!$conn){&nbsp; &nbsp; &nbsp; &nbsp; echo "Connection failed:" . mysqli_connect_error();&nbsp; &nbsp; }&nbsp; &nbsp; //Writing query for database.&nbsp; &nbsp; $sql = "SELECT `First Name`,`Last Name`,Emails,`Date Created` FROM clientinfo ORDER BY `Date Created`";&nbsp; &nbsp; //Querying and getting results&nbsp; &nbsp; $result = mysqli_query($conn,$sql);&nbsp; &nbsp; $informed = mysqli_fetch_all($result, MYSQLI_ASSOC);&nbsp;?><!DOCTYPE html><html><head><div class="Contained"><div class="row">&nbsp; &nbsp; <?php foreach($informed as $inform){?>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col s6 medium-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card z-depth-0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-content center">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h6><?php echo htmlspecialchars($inform['First Name']); ?></h6>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div><?php echo htmlspecialchars($inform['Last Name']);?></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-action right-align">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="brand-text" href="#">More Info</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; <?php }?></div></div><title> Email and Name List </title></head><body>&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>First Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Last Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Emails</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Date Created</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($result->num_rows>0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while($row = $result->fetch_assoc()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr><td>" . $row["First Name"] . "</td><td>" .&nbsp; $row["Last Name"] . "</td><td>" . $row["Emails"] . "</td><td>" . $row["Date Created"] . "</td></tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<tr><td rowspan=\"5\">0 result</td></tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </table></body><?php&nbsp;&nbsp; &nbsp; // Freeing result from the memory.&nbsp; &nbsp; mysqli_free_result($result);&nbsp; &nbsp; mysqli_close($conn);?>
打开App,查看更多内容
随时随地看视频慕课网APP