将SQL数据放入HTML表。表头是重复的

时间:2016-01-20 07:34:35

标签: php html mysql mysqli

我试图从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>**

每次插入数据时,表头都会与表数据一起重复。我使用&#34; mysql_connect&#34;而不是&#34; mysqli&#34; 。请帮我解决这个问题。

这是错误的图片: - http://i.stack.imgur.com/1yOZ3.png

1 个答案:

答案 0 :(得分:4)

您需要在(循环外)

之前放置表头

您的标题会针对每条记录重复。

应该只来一次。

更正后的代码:

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