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

时间:2015-08-12 13:35:01

标签: 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>

4 个答案:

答案 0 :(得分:2)

首先吸引大脑,然后开始编码。

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

<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>

答案 1 :(得分:0)

将表创建和结束表的一部分保留在while循环中,如下所示:

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>";

 while ($row = mysqli_fetch_array($result)) {....
  //     print the records <Tr> <Td...... 
  }

echo "</table>";

答案 2 :(得分:0)

您没有正确执行循环。事实上,您的个人资料表中的每条记录都会创建<table>标记,但这并不好。

这样做的正确方法是这样的:

<html>
<head>
</head>
<body>

<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit(); //Exit not to display the table since there is an mysqli error
}
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>
    <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>
    <tr>";
$result = mysqli_query($con, "SELECT * FROM profiles");
while ($row = mysqli_fetch_array($result)) {

    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>

希望有所帮助。

答案 3 :(得分:0)

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>";

这是你的问题。循环运行时,每次运行时都会生成新表,因此您需要将此代码放在while循环之外,就在它之前。在while循环中,您只需要在现有表中添加新列的代码。