使用PHP以HTML格式从表格式获取和显示数据库中的数据

时间:2015-05-02 09:02:59

标签: php html

我已完成提取和显示数据。但问题是,它在html表的同一行显示获取行的数据。 例如:我想像这样显示

                  ID | Name | Desination
                   ......................
                   1 | ABC | Developer
                   2 | PQR | Tester
                   3 | XYZ | Developer

但它显示为 -

                   ID | Name | Desination
                   ......................
                    1 | ABC | Developer   2 | PQR | Tester   3 | XYZ | Developer

我做过类似的事情 -

$sql = " SELECT candidate.cand_number,candidate.cand_fname,candidate.cand_desc FROM candidate ".$join.' where '.$condition;
$result = mysql_query($sql) or die(mysql_error()); 

用表格的显示格式纠正我。

 <div class="box-body table-responsive no-padding">
     <table class="table table-hover">
          <tr>
              <th>ID</th>
              <th>Name</th>
              <th>Designation</th>
          </tr>
          <tr>
              <?php

                   If(mysql_num_rows($result)>0)
                   {
                     while($row=mysql_fetch_array($result))
                     {  

                ?>
                  <td><?php echo $row['cand_number']; ?></td> 
                  <td><?php echo $row['cand_fname']; ?></td> 
                  <td><?php echo $row['cand_desc']; ?></td> 
                <?php

                }
                }
                 ?>

              </tr>
       </table>
    </div>

4 个答案:

答案 0 :(得分:5)

您需要重复行而不是列

 <?php
If (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_array($result)) {
        ?>
        <tr>
            <td><?php echo $row['cand_number']; ?></td> 
            <td><?php echo $row['cand_fname']; ?></td> 
            <td><?php echo $row['cand_desc']; ?></td> 
        </tr>
        <?php
    }
}
?>

答案 1 :(得分:0)

您需要添加到while循环中。因此,您的所有数据都打印在第一行内。

<?php
If (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_array($result)) {
        ?>
        <tr>
            <td><?php echo $row['cand_number']; ?></td> 
            <td><?php echo $row['cand_fname']; ?></td> 
            <td><?php echo $row['cand_desc']; ?></td> 
        </tr>
        <?php
    }
}
?>

答案 2 :(得分:0)

使用此HTML并尝试

<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<tr>
   <th>ID</th>
   <th>Name</th>
   <th>Designation</th>
</tr>

          <?php

               If(mysql_num_rows($result)>0)
               {
                 while($row=mysql_fetch_array($result))
                 {  

            ?>
             <tr>
              <td><?php echo $row['cand_number']; ?></td> 
              <td><?php echo $row['cand_fname']; ?></td> 
              <td><?php echo $row['cand_desc']; ?></td> 
            </tr>
            <?php

            }
            }
             ?>


   </table>
</div>

答案 3 :(得分:0)

除了其他答案:不要忘记sanitise $ join和$ condition。 这样可以防止SQL注入。

相关问题