2

解决

完美运行!这是我的最终代码:

<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 表。我计划制作一个包含mysql表列名的thead和一个包含mysql表结果集的tbody 。SQL 查询从表中选择几列,并设置了默认限制。

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

它显示在 each 中回显的列名,然后跳过第一条记录并成功回显第二行。例如:

| 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>

我现在感觉很健忘=/

4

1 回答 1

7

先把你的数据倒回去!!

mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result)) {
...
于 2012-12-12T12:23:01.107 回答