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

时间:2022-09-25 21:41:09

I have done with fetching and display the data. But the problem is ,its display the data of fetched row in the same row of html table. For example : i want to display like this

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

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

But its showing as -

但其显示为 -

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

I have done something like this-

我做过类似的事 -

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

Correct me with the display format of table.

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

 <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 个解决方案

#1


You need to repeat row not column

您需要重复行而不是列

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


Use this HTML and try

使用此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


You need to add into the while loop. Because of this your all data is printed inside first row.

您需要添加到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
    }
}
?>

#4


In addition to the other answers: don't forget to sanitise $join and $condition. This to prevent SQL injection.

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

#1


You need to repeat row not column

您需要重复行而不是列

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


Use this HTML and try

使用此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


You need to add into the while loop. Because of this your all data is printed inside first row.

您需要添加到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
    }
}
?>

#4


In addition to the other answers: don't forget to sanitise $join and $condition. This to prevent SQL injection.

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