【问题标题】:How to show add one row of a mysql Table?如何显示添加一行mysql表?
【发布时间】:2020-05-01 17:36:48
【问题描述】:

您好,我想显示表格中一行的所有数据。但我只能显示表格的 1 列。

function getAllRecipes(): array {
    global $connection;
    $query = "SELECT * FROM recipe";

    $stmt = $connection->prepare($query);
    $stmt->execute();

    return $stmt->fetchAll();
}

$recipe = getAllRecipes();

<?php foreach ($recipe as $recip) : ?>
<table>
    <tr>
        <td><?= **$recip["id"];** ?></td>  <----here
    </tr>
</table>

这是结果

我想要所有的行:

你知道怎么做吗?

感谢您的帮助

【问题讨论】:

  • &lt;td&gt;&lt;?= **$recip["title"];** ?&gt;&lt;/td&gt; 并对所有列重复
  • 您想在不同的 中显示来自不同列的所有值?
  • @bestprogrammerintheworld 是的
  • 只有重复td才有可能吗?
  • 你已经在那里做了一栏。为什么不添加其他列???

标签: php mysql datatable


【解决方案1】:

基于您的“答案”中的“我只想通过一行显示所有包含的行”。

$arr_values = array_values($recipe);
$html = '<table><tr><th>' . implode('</th><th>', $arr_values) . '</tr></table>';
echo $html;

如果您只想要键(列)的名称:

$arr_keys = array_keys($recipe);
$html = '<table><tr><th>' . implode('</th><th>', $arr_keys) . '</tr></table>';
echo $html;

【讨论】:

  • 没问题。我很高兴能帮上忙!
猜你喜欢
相关资源
最近更新 更多
热门标签