首页 > 解决方案 > 从外部 php 文件在 html 文件中添加行

问题描述

我正在尝试找到如何在 html 中的表中插入行,但我想使用外部 php 文件来完成,但可能是另一种解决方案,因为目的是从数据库中获取表并写入它下降到一个html表。

我试图在互联网上查看,但我没有找到与我正在寻找的东西相匹配的东西。

标签: javascriptphphtml

解决方案


你的意思是像这里的“示例1”(https://www.php.net/manual/en/mysqli-result.fetch-assoc.php)吗?

您只需要添加 html 标签即可创建 html 表(table、td、...)。

编辑:

此示例包含 html 标记,应该执行您想要的操作:

<html>
...
<body>
<?php

   $mysqli = new mysqli("localhost", "my_user", "my_password", "database");

   $query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

   if ($result = $mysqli->query($query)) {

       echo "<table>";
       /* fetch associative array */
       while ($row = $result->fetch_assoc()) {
           echo "<tr><td>" . $row['Name'] . "</td><td>" . $row['CountryCode'] . "</td></tr>";
       }
       echo "</table>";

       /* free result set */
       $result->free();
   }
?>
</body>

推荐阅读