使用PHP以HTML格式从表格式获取和显示数据库中的数据
问题描述:
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
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>
我已经完成了获取和显示数据的工作。 但问题是,它在html表的同一行显示获取行的数据。 例如:我想显示如下 p>
ID | 名称| Desination
......................
1 | ABC | 开发人员
2 | PQR | 测试者
3 | XYZ | Developer
code> pre>
但它显示为 - p>
ID | 名称| Desination
......................
1 | ABC | 开发人员2 | PQR | 测试者3 | XYZ | Developer
code> pre>
我做过类似的事情 - p>
$ sql =“SELECT candidate.cand_number,candidate。 cand_fname,candidate.cand_desc来自候选人“。$ join。” 其中'。$ condition;
$ result = mysql_query($ sql)或die(mysql_error());
code> pre>
使用表格的显示格式纠正我。 p>
&lt; div class =“box-body table-responsive no-padding”&gt;
&lt; table class =“table table-hover”&gt;
&lt; ; tr&gt;
&lt; th&gt; ID&lt; / th&gt;
&lt; th&gt;名称&lt; / th&gt;
&lt; th&gt;名称&lt; / th&gt;
&lt; / tr&gt;
&lt; tr&gt; \ n&lt;?php
如果(mysql_num_rows($ result)&gt; 0)
{
while($ row = mysql_fetch_array($ result))
{
?&gt;
&lt; td&gt;&lt;?php echo $ row ['cand_number']; ?&GT;&LT; / TD&GT;
&lt; td&gt;&lt;?php echo $ row ['cand_fname']; ?&GT;&LT; / TD&GT;
&lt; td&gt;&lt;?php echo $ row ['cand_desc']; ?&GT;&LT; / TD&GT;
&lt;?php
}
}
?&gt;
&lt; / tr&gt;
&lt; / table&gt;
&lt; / div&gt;
code> pre>
div>
答
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
}
}
?>
答
You need to add into the while loop. Because of this your all data is printed inside first row.
<?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
}
}
?>
答
Use this HTML and try
<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>
答
In addition to the other answers: don't forget to sanitise $join and $condition. This to prevent SQL injection.