php - 将 SQL 数据放入 HTML 表中。表格标题重复

标签 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>**

每次插入数据时,表头都会与表数据重复。我用的是“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/

相关文章:

php - 如何使我的 html <select> 与 php 保持一致?

javascript - 如何在 HTML 表单和 javascript 之间进行通信?

php - 格式化sql查询的十进制输出

mysql - 如何从csv中插入记录到mysql?

php - Div 并非一直在 foreach 循环中申请

php - ajax请求后php函数未接收变量

PHP 输入 GET 变量清理

javascript - "merging"图片和文字为一体

html - 如何使用百分比划分 HTML 表格

php - mysql IN 参数