【问题标题】:Echo MySQL result as column heading? in PHP [closed]Echo MySQL 结果作为列标题?在 PHP [关闭]
【发布时间】:2015-03-30 20:35:16
【问题描述】:

我有一个表格中的日期列表,我想使用 php 作为列名输出它们

所以如果表的值是 1,2 和 3

我想创建一个包含 3 列和 1,2 和 3 作为列名的表

【问题讨论】:

  • 既太宽泛又不清楚你在问什么。
  • 我想用mysql结果作为列名

标签: php mysqli


【解决方案1】:
<?php
function db_select_with_col()
{
$rows = array();
$result = db_query("SELECT
student.StudentID,
student.`Name`,
attendance.Date,
CASE WHEN attendance.StudentID IS NOT NULL THEN 'Present' ELSE 'Absent' END as Attendance_Status
FROM
student
Left JOIN attendance ON student.StudentID = attendance.StudentID
WHERE
attendance.Date = '2015-03-30'");

// If query failed, return `false`
if ($result === false) {
    return false;
}


// If query was successful, retrieve all the rows into an array
while ($row = mysqli_fetch_assoc($result)) {
    $rows[] = $row;
}
$colNames = array_keys(reset($rows));

foreach ($colNames as $colName) {
    echo "<th>$colName</th>";
}

foreach ($rows as $row) {
    echo "<tr>";
    foreach ($colNames as $colName) {
        echo "<td>" . $row[$colName] . "</td>";
    }
    echo "</tr>";
}
}

?>

感谢 [这里] (https://stackoverflow.com/a/14430366/4680117) 的回答,我想出了如何做到这一点

【讨论】:

    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2016-11-23
    • 2023-03-21
    • 2010-11-26
    • 2013-05-04
    相关资源
    最近更新 更多