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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30000403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:28:38  来源:igfitidea点击:

Fetch and display data from database in table format in HTML form using PHP

phphtml

提问by vinod

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>

回答by Pranay Aher

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

回答by Sagar

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>

回答by Sagar

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

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

回答by Codelord

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