0

我正在尝试从 MySQL 数据库中获取数据。我想随着数据库中数据的增加而增加行数,但标题行应该只有一个。像典型的表,但这里只需要动态增加#row。

    <?php 
    mysql_connect ("localhost","root","root123"); 
    mysql_select_db ("iterrordb"); 

    $sql = "select * from record"; 
       $result = mysql_query ($sql); 
        $num=mysql_numrows($result);
        while ($row = mysql_fetch_array($result)) 
              { 
              $RecID= $row["RecordID"]; 
              $DispName= $row["Name"]; 
              $date= $row["date"]; 
              $fleet= $row["phone"]; 
              $issue= $row["issue"]; 
              $issueRelatedTo= $row["issuRelatedTo"]; 
              } 
        //HTML CODE is start from here

        echo "<table border=\"1\" bordercolor=\"#0066FF\" style=\"background-color:#CCFFFF\" width=\"50%\" cellpadding=\"1\" cellspacing=\"0\">";
        echo"<tr>";
        echo"<td>Record ID</td>";
        echo"<td>Name</td>";
        echo"<td>Date</td>";
        echo"<td>Phone</td>";
        echo"<td>Issue</td>";
        echo"<td>Issue Related To</td>";
        echo"</tr>";
        echo"<tr>";     
        for ($i=0; $i<6; $i++)  
            {
                echo"<td> $row[$i]</td>";
            }
        echo"</tr>";

        echo"</table>";

?>
4

2 回答 2

1

将您的代码重写为

<?php 
mysql_connect ("localhost","root","root123"); 
mysql_select_db ("iterrordb"); 

$sql = "select * from record"; 
   $result = mysql_query ($sql); 
    $num=mysql_numrows($result);
    //HTML CODE is start from here

    echo "<table border=\"1\" bordercolor=\"#0066FF\" style=\"background-color:#CCFFFF\" width=\"50%\" cellpadding=\"1\" cellspacing=\"0\">";
    echo"<tr>";
    echo"<td>Record ID</td>";
    echo"<td>Name</td>";
    echo"<td>Date</td>";
    echo"<td>Phone</td>";
    echo"<td>Issue</td>";
    echo"<td>Issue Related To</td>";
    echo"</tr>";

        while ($row = mysql_fetch_array($result)) 
          { 
          $RecID= $row["RecordID"]; 
          $DispName= $row["Name"]; 
          $date= $row["date"]; 
          $fleet= $row["phone"]; 
          $issue= $row["issue"]; 
          $issueRelatedTo= $row["issuRelatedTo"]; 
          echo"<tr>"; 
         for ($i=0; $i<6; $i++)  
        {
            echo"<td> $row[$i]</td>";
        }
        echo"</tr>";
       }
    echo"</table>";

 ?>

在当前上下文中,以下代码似乎不相关,但无论如何我没有删除它

      $RecID= $row["RecordID"]; 
      $DispName= $row["Name"]; 
      $date= $row["date"]; 
      $fleet= $row["phone"]; 
      $issue= $row["issue"]; 
      $issueRelatedTo= $row["issuRelatedTo"]; 
于 2012-08-08T17:01:55.167 回答
1

我不确定你的意思。但是,如果您想在查询结果中获得行数,这将达到目的:

$num_rows = mysql_num_rows($result);

如果您想知道“while”循环中的当前行号是多少,只需添加一个迭代器。

如果你想动态更新你的 html 表。使用 jQuery:

$('#my_table').empty().load("/examples/getTableRows.php");

您可以每隔 5 秒调用一次此函数来更新表格。

于 2012-08-08T17:05:47.557 回答