PHP 不将数据库打印为表

标签 php html mysql sql

我正在尝试将 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 lang="en-US">
    <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>
    <body>
        <table>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Emails</th>
                <th>Date Created</th>
            </tr>
        </table>
    </body>
</html>

Output in browser gyazo:

最佳答案

您必须更改 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 
Created`";

//Querying and getting results

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

if ($result->num_rows>0){
echo '
<table>
<tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Emails</th>
    <th>Date Created</th>
</tr>';
    while($row = $result->fetch_assoc()){
        echo "<tr> ";
        echo "<td>" . $row["First Name"] . "</td>";
        echo "<td>" . $row["Last Name"] . "</td>";
        echo "<td>" . $row["Date Created"] . "</td>";
        echo "</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);

?>

</body>

另一个问题,您确定是 $row["First Name"] 而不是 $row["First_Name"] 吗?
最后一个技巧学习如何准备 stm 来防止 sql 注入(inject)

关于PHP 不将数据库打印为表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59784792/

相关文章:

php - 通过 PHP 的 HTTP 身份验证 - 未设置 PHP_AUTH_USER?

php - 在 PHP 中缓存变量

php - PHP 中视频上传的最佳方法是什么

javascript - 查找离位置最近的 html 元素(相对或绝对)

html - 如何对 <div> 标签的这种现象进行理论化

php - GIT Hook疑惑

javascript - 未获取单击的 href 标签的索引值

mysql - 在 mysql 5.5 中解析 JSON

Mysql多重连接对self和其他表性能的影响

mysql - 匹配约束后从 MySQL 表中删除