如何使用 INNER JOIN 在一个 HTML table 行中打印来自两个 MySQL table 的动态数据?
How do I use an INNER JOIN to print DYNAMIC data from two MySQL tables in one HTML table row?
我正在尝试
- 使用 INNER JOIN 从两个不同的表中获取数据;
- 从其中一个表中动态获取列的名称;
- 动态地将列名放入 HTML;
- 将连接的查询显示为单个 HTML 行。
这是我正在使用的,但不起作用:
$table="tableUname";
//_tableUname holds identifiers such as username, name, and email
//_tableDetails holds fifty columns of details...
//...columns named Detail1-Detail50
//--because columnn names simply increment.
//Therefore, an incrementing dynamic function is used to specify column names.
// _tableDetails has an id column which references id column in TableUname...
// so tableUname.id=tableDetails.id
function outputMySQLToHTMLTable(mysqli $mysqli, $table)
{
$res = $mysqli->query('SELECT username, name, email FROM tableUname
INNERJOIN tableDetails ON tableUname.id=tableDetails.id
GROUP BY id');
$data = $res->fetch_all(MYSQLI_ASSOC);
//below: HTML table printout
echo '<head>
<style>
tr:nth-of-type(even) {
background-color:#cccccc;
}
</style>
</head>';
echo '<body>';
echo '<table
style="tr:nth-child(even){
background-color: #cccccc;}">';
// Display table header
echo '<thead>';
echo '<tr>';
//below: grabbing columns
foreach ($res->fetch_fields() as $column) {
echo '<th>'.htmlspecialchars($column->name).'</th>';
}
echo '</tr>';
echo '</thead>';
// If there is data then display each row
if ($data) {
foreach ($data as $row) {
echo '<tr>';
// data from JOINED tableUname
echo "<td>".$row["username"]."</td>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["email"]."</td>";
// end data from JOINED tableUname
// start DYNAMIC data from tableDetails, while on same HTML table row
foreach ($row as $cell) {
echo '<td>'.htmlspecialchars($cell).'</td>';
}
echo '</tr>';
}
} else {
echo '<tr><td colspan="'.$res->field_count.'">
No records in the table! </td></tr>';
}
echo '</table>';
}
echo '</body>';
outputMySQLToHTMLTable($mysqli, $DbUser);
这不起作用——我做错了什么?谢谢! :)
该函数不使用 $table
参数,因此我已将其删除。
我已将 tableDetails.*
添加到查询的 SELECT
列表中。
您可以使用一个循环来显示所有列,您不必将固定列与动态列分开处理。
function outputMySQLToHTMLTable(mysqli $mysqli)
{
$res = $mysqli->query('SELECT u.username, u.name, u.email, d.*
FROM tableUname AS u
INNERJOIN tableDetails AS d ON u.id=d.id
GROUP BY id');
$data = $res->fetch_all(MYSQLI_ASSOC);
//below: HTML table printout
echo '<head>
<style>
tr:nth-of-type(even) {
background-color:#cccccc;
}
</style>
</head>';
echo '<body>';
echo '<table
style="tr:nth-child(even){
background-color: #cccccc;}">';
// Display table header
echo '<thead>';
echo '<tr>';
//below: grabbing columns
foreach ($res->fetch_fields() as $column) {
echo '<th>'.htmlspecialchars($column->name).'</th>';
}
echo '</tr>';
echo '</thead>';
// If there is data then display each row
if ($data) {
foreach ($data as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>'.htmlspecialchars($cell).'</td>';
}
echo '</tr>';
}
} else {
echo '<tr><td colspan="'.$res->field_count.'">
No records in the table! </td></tr>';
}
echo '</table>';
}
echo '</body>';
outputMySQLToHTMLTable($mysqli);
我正在尝试
- 使用 INNER JOIN 从两个不同的表中获取数据;
- 从其中一个表中动态获取列的名称;
- 动态地将列名放入 HTML;
- 将连接的查询显示为单个 HTML 行。
这是我正在使用的,但不起作用:
$table="tableUname";
//_tableUname holds identifiers such as username, name, and email
//_tableDetails holds fifty columns of details...
//...columns named Detail1-Detail50
//--because columnn names simply increment.
//Therefore, an incrementing dynamic function is used to specify column names.
// _tableDetails has an id column which references id column in TableUname...
// so tableUname.id=tableDetails.id
function outputMySQLToHTMLTable(mysqli $mysqli, $table)
{
$res = $mysqli->query('SELECT username, name, email FROM tableUname
INNERJOIN tableDetails ON tableUname.id=tableDetails.id
GROUP BY id');
$data = $res->fetch_all(MYSQLI_ASSOC);
//below: HTML table printout
echo '<head>
<style>
tr:nth-of-type(even) {
background-color:#cccccc;
}
</style>
</head>';
echo '<body>';
echo '<table
style="tr:nth-child(even){
background-color: #cccccc;}">';
// Display table header
echo '<thead>';
echo '<tr>';
//below: grabbing columns
foreach ($res->fetch_fields() as $column) {
echo '<th>'.htmlspecialchars($column->name).'</th>';
}
echo '</tr>';
echo '</thead>';
// If there is data then display each row
if ($data) {
foreach ($data as $row) {
echo '<tr>';
// data from JOINED tableUname
echo "<td>".$row["username"]."</td>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["email"]."</td>";
// end data from JOINED tableUname
// start DYNAMIC data from tableDetails, while on same HTML table row
foreach ($row as $cell) {
echo '<td>'.htmlspecialchars($cell).'</td>';
}
echo '</tr>';
}
} else {
echo '<tr><td colspan="'.$res->field_count.'">
No records in the table! </td></tr>';
}
echo '</table>';
}
echo '</body>';
outputMySQLToHTMLTable($mysqli, $DbUser);
这不起作用——我做错了什么?谢谢! :)
该函数不使用 $table
参数,因此我已将其删除。
我已将 tableDetails.*
添加到查询的 SELECT
列表中。
您可以使用一个循环来显示所有列,您不必将固定列与动态列分开处理。
function outputMySQLToHTMLTable(mysqli $mysqli)
{
$res = $mysqli->query('SELECT u.username, u.name, u.email, d.*
FROM tableUname AS u
INNERJOIN tableDetails AS d ON u.id=d.id
GROUP BY id');
$data = $res->fetch_all(MYSQLI_ASSOC);
//below: HTML table printout
echo '<head>
<style>
tr:nth-of-type(even) {
background-color:#cccccc;
}
</style>
</head>';
echo '<body>';
echo '<table
style="tr:nth-child(even){
background-color: #cccccc;}">';
// Display table header
echo '<thead>';
echo '<tr>';
//below: grabbing columns
foreach ($res->fetch_fields() as $column) {
echo '<th>'.htmlspecialchars($column->name).'</th>';
}
echo '</tr>';
echo '</thead>';
// If there is data then display each row
if ($data) {
foreach ($data as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>'.htmlspecialchars($cell).'</td>';
}
echo '</tr>';
}
} else {
echo '<tr><td colspan="'.$res->field_count.'">
No records in the table! </td></tr>';
}
echo '</table>';
}
echo '</body>';
outputMySQLToHTMLTable($mysqli);