我正在尝试从我的 mySQL 数据库中获取数据并将它们放入 HTML 表中。在互联网上搜索了很多之后,我找不到适合我的代码。
目前,我有这段代码
**<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$user = "root";
$password = "";
$db = mysql_connect('localhost', $user, $password);
if (!$db) {
die('Not connected : ' . mysql_error());
}
$dbname = "btcx2";
$db_select = mysql_select_db($dbname,$db);
if (!$db_select) {
die("Database selection also failed miserably: " . mysql_error());
}
$results = mysql_query("SELECT btcaddress, btclink, btcreceived , btctarnsaction, lastdeposit, reg_date, uptodate , btcdoubble FROM btctrans");
while($row = mysql_fetch_array($results)) {
?>
<table style="width:95%;height:95%;" border="2">
<thead>
<tr>
<th><strong>Date</strong></th>
<th><strong>BTC Address</strong></th>
<th><strong>Amount Deposited</strong></th>
<th><strong>Amount Returned</strong></th>
<th><strong>Transaction</strong></th>
</tr>
</thead>
<tbody>
<tr>
[<td><font color="red"> <?php echo $row\['reg_date'\]?></font></td>
<td> <a href="<?php echo $row\['btclink'\]?>"><?php echo $row\['btcaddress'\]?></a></td>
<td><font color="red">BTC: <?php echo $row\['btcreceived'\]?> </font></td>
<td><font color="red">BTC: <?php echo $row\['btcdoubble'\]?></font></td>
<td><font color="red"> <?php echo $row\['btctarnsaction'\]?></font></td>][1]
</tr>
<?php
}
?>
</tbody>
</table></center>**
每次插入数据时,表头都会与表数据重复。我用的是“mysql_connect”而不是“mysqli”。请有人解决这个问题帮助我解决这个问题。
这是错误的图片:- http://i.stack.imgur.com/1yOZ3.png
最佳答案
您需要在(循环外)之前放置表格标题
每条记录的标题都重复。
应该只来一次。
更正后的代码:
<table style="width:95%;height:95%;" border="2">
<thead>
<tr>
<th><strong>Date</strong></th>
<th><strong>BTC Address</strong></th>
<th><strong>Amount Deposited</strong></th>
<th><strong>Amount Returned</strong></th>
<th><strong>Transaction</strong></th>
</tr>
</thead>
<?php
while($row = mysql_fetch_array($results)) {
?>
关于php - 将 SQL 数据放入 HTML 表中。表格标题重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34894043/