-1

使用此代码,我可以在 td-tag 中显示文本“Array”。我怎样才能得到真正的价值?

<tbody>
        <tr>
        <?php
        $STH = $DBH->prepare("SELECT * FROM customers");
        $STH->execute();
        $result = $STH->fetchall();

        foreach($result as $key => $value) {
            echo "<td>$value</td>";
        }

        ?>
        </tr>
</tbody>
4

2 回答 2

3
foreach($result as $key => $value) {
    echo "<td>{$value['field_name']}</td>";
}

$value是一个数组,因为$result应该是一个二维数组。所以你需要这样打电话。

echo "<td>{$value['field_name']}</td>";

您可以添加一个额外的foreach.

foreach($result as $key => $inner_arr) {
    echo '<tr>';
    foreach($inner_arr as $field_name => $field_value) {
        echo "<td>{$field_value}</td>";
    }
    echo '</tr>';
}
于 2013-08-28T08:59:47.403 回答
0

你需要一个额外的层

<tbody>

        <?php
        $STH = $DBH->prepare("SELECT * FROM customers");
        $STH->execute();
        $result = $STH->fetchall();

        foreach($result as $key => $value) {
            echo '<tr>'; // as we have more rows
            // here key is a rownumber value is the full row
            //echo "<td>$value</td>";
            foreach($value as $name => $data) {
              echo "<td>$data</td>";
            }
            echo '</tr>';
        }

        ?>

</tbody>
于 2013-08-28T09:03:16.090 回答