php - 在表中显示 MYSQL 数据库中的数据(不单独)

标签 php mysql database

我想在表格中显示数据库中的值。我已经成功地显示了下面的值。但这些表都是彼此分开的。

如何将它们全部合并到一张表中?

<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");
while ($row = mysqli_fetch_array($result)) {
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
    echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> 
<th>Age</th>
    <th>gender</th>
    <th>hometown</th>
    <th>University</th>
    <th>Occupation</th> </tr>";
    echo "<tr>";



echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['university'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";

         echo "</tr>";
    echo "</table>";
  echo "</div>";
}




mysqli_close($con);

?>

</body>
</html>

最佳答案

首先动脑筋,然后开始编码。

您只想将表行作为循环的一部分输出。

<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");

echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'>";
echo "<tr> <th>Firstname</th> 
           <th>Lastname</th> <th>Age</th>
           <th>gender</th>
           <th>hometown</th>
           <th>University</th>
           <th>Occupation</th> 
      </tr>";

while ($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['first_name'] . "</td>";
    echo "<td>" . $row['last_name'] . "</td>";
    echo "<td>" . $row['age'] . "</td>";
    echo "<td>" . $row['gender'] . "</td>";
    echo "<td>" . $row['hometown'] . "</td>";
    echo "<td>" . $row['university'] . "</td>";
    echo "<td>" . $row['occupation'] . "</td>";
    echo "</tr>";
}
echo "</table>";
echo "</div>";




mysqli_close($con);

?>

</body>
</html>

关于php - 在表中显示 MYSQL 数据库中的数据(不单独),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31966799/

相关文章:

php - 发送 cache-control/expires/pragma 与 404 响应 - 现代浏览器有效/理解?

MySQL 服务器不通过 phpmyAdmin 创建存储过程

mysql - 如何在 MySQL 中不四舍五入地获得精确的十进制值

ruby-on-rails - rake 数据库 :schema:load works in development but not production

database - 如何执行 YB 的 k8s/helm 设置?

java - 如何在android中没有LiveData的情况下使用房间和数据库?

php - Laravel Eloquent 更新 created_at 值

javascript - 带有 javascript 的 Ajax 无法在 wamp 服务器上运行

mysql - 在mac终端中打开mamp mysql

php - 如何从网络向应用程序发送 FCM 通知