PHP回显mysql表的html表头的列名

时间:2012-12-12 12:20:33

标签: php mysql html-table

分辨

完美的作品!这是我的最终代码:

<table>
  <thead>
    <tr>
      <?php
      $row = mysql_fetch_assoc($result);
            foreach ($row as $col => $value) {
                echo "<th>";
                echo $col;
                echo "</th>";
            }
      ?>
      <th>Edit</th>
    </tr>
  </thead>
  <tbody>
    <?php
  // Write rows
  mysql_data_seek($result, 0);
    while ($row = mysql_fetch_assoc($result)) {
        ?>
    <tr>
      <?php         
    foreach($row as $key => $value){
        echo "<td>";
        echo $value;
        echo "</td>";
    }
    ?>
      <td><button id="edit_project_button(<?php echo $row['ID']; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row['ID']; ?>)">Edit</button></td>
    </tr>
    <?php } ?>
  </tbody>
</table>

我希望适当地使用mysql_fetch函数回显一个HTML表。我计划使 thead 包含mysql表列名和 tbody 以包含mysql表结果集。 SQL查询从表中选择几列,并设置默认限制。

问题:它似乎没有打印第一行表数据,其他所有内容都显示(缺少记录#1)

它会在每个列表中显示with列名称,然后跳过第一个记录,并成功回显第二行。例如:

| id | firstname | lastname | date_start | date_end   | clientid | members | edit          |
|  2 | Cal       | Clark    | 2012-12-12 | 2012-12-12 | 22       | Rob     | (edit button) |
|  3 | Rob       | Robin    | 2012-12-12 | 2012-12-12 | 33       | Cal     | (edit button) |

我100%确定第一条记录将在我的phpmyadmin中显示。

这是我的代码:

<table>
  <thead>
    <tr>
      <?php
        $row = mysql_fetch_assoc($result);
            foreach ($row as $col => $value) {
                echo "<th>";
                echo $col;
                echo "</th>";
            }

      ?>
      <th>Edit</th>
    </tr>
  </thead>
  <?php
  // Write rows
  while ($row = mysql_fetch_array($result)) {
    ?>
  <tr>
    <td><?php echo $row[0]; ?></td>
    <td><?php echo $row[1]; ?></td>
    <td><?php echo $row[2]; ?></td>
    <td><?php echo $row[3]; ?></td>
    <td><?php echo $row[4]; ?></td>
    <td><?php echo $row[5]; ?></td>
    <td><?php echo $row[6]; ?></td>
    <td><button id="edit_project_button(<?php echo $row[0]; ?>)" class="edit-project-button edit button" onclick="editproject(<?php echo $row[0]; ?>)">Edit</button></td>
  </tr>
  <?php } ?>
</table>

我现在感到如​​此无视= /

1 个答案:

答案 0 :(得分:7)

首先回放您的数据!!

mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result)) {
...