【发布时间】:2016-05-07 03:48:19
【问题描述】:
是否可以将 MySQL 表中多列的数据合并到 HTML 表中的单个列中,其中每条记录将是 HTML 表中的新行?
在这个例子中,MySQL 中的一个表有两列(Col1,Col2)。 Col1 中的数据显示在 HTML 表的第一列中。 MySQL 中 Col2 的数据显示在 HTML 表的第二列中。换言之,HTML 表与 MySQL 表的布局相匹配。
<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($sql_query)) {
$col1 = $row['col1'];
$col2 = $row['col2'];
echo "<table>";
echo "<tr>";
echo "<td> $col1 </td>";
echo "<td> $col2 </td>";
echo "</tr>";
echo "</table>";
}
?>
MySQL 表:
| - - - - | - - - - - - - |
| Col1 | Col2 |
| - - - - | - - - - - - - |
| Blue | Car |
| Green | Truck |
| Yellow | Van |
| - - - - | - - - - - - - |
HTML 表格:
| - - - - | - - - - - - - |
| Column1 | Column2 |
| - - - - | - - - - - - - |
| Blue | Car |
| Green | Truck |
| Yellow | Van |
| - - - - | - - - - - - - |
如果将 $col1 和 $col2 放在单个 TD 标记内,这确实会使 $col1 和 $col2 都显示在 HTML 表的 Col1 中。但是,$col1 和 $col2 都显示在同一个单元格中。
<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($sql_query)) {
$col1 = $row['col1'];
$col2 = $row['col2'];
echo "<table>";
echo "<tr>";
echo "<td> $col1 $col2 </td>";
echo "</tr>";
echo "</table>";
}
?>
HTML 表格:
| - - - - - - - - - - - - |
| Column1 |
| - - - - - - - - - - - - |
| Blue Car |
| Green Truck |
| Yellow Van |
| - - - - - - - - - - - - |
是否可以在 HTML 表的 Column1 中回显 $Col1 和 $Col2 并使每条记录在 HTML 表中的自己的行中?
| - - - - - - - - - - - - |
| Column1 |
| - - - - - - - - - - - - |
| Blue |
| Green |
| Yellow |
| Car |
| Truck |
| Van |
| - - - - - - - - - - - - |
【问题讨论】:
-
你想要你问题中最后一个数字的结果吗??