繁体   English   中英

如何使用PHP html以表格形式显示数据库?

[英]How do I display my database in table form using PHP html?

如何以表格形式显示数据库。 这是我的代码:

<?php

$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"order");
$res=mysqli_query($link,"select * from ordersum");
while($row=mysqli_fetch_array($res))
{

echo $row["name"]." ".$row["email"]." ".$row["content"]." ".$row["date"]." ".$row["amount"];
echo "<br>";

}

?>

在此处输入图片说明

试试这个,希望能对您有所帮助。

<table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Content</th>
                <th>Date</th>
                <th>Amount</th>
            </tr>
        </thead>
    <tbody>

<?php
   $link=mysqli_connect("localhost","root","");
   mysqli_select_db($link,"order");
   $res=mysqli_query($link,"select * from ordersum");

   while($row=mysqli_fetch_array($res)){
        echo "<tr>
              <td>".$row["name"]."</td>
              <td>".$row["email"]."</td>
              <td>".$row["content"]."</td>
              <td>".$row["date"]."</td>
              <td>".$row["amount"]."</td>
              </tr>";
   }
?>
   </tbody>
</table>

您的代码应如下所示:

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Email</th>
            <th>Content</th>
            <th>Date</th>
            <th>Amount</th>
        </tr>
    </thead>
    <tbody>
        <tr>
<?php
// your connection code here
while($row=mysqli_fetch_array($res)) { ?>
            <td><?php echo $row["name"];?></td>
            <td><?php echo $row["email"];?></td>
            <td><?php echo $row["content"];?></td>
            <td><?php echo $row["date"];?></td>
            <td><?php echo $row["amount"];?></td>
<?php } ?>
        </tr>
    </tbody>
</table>

尽量不要回显HTML标记,这不是一个好习惯,相反,请关闭php,渲染html,回显您的PHP值,并确保在其末尾关闭循环(同时for,foreach)。

我尝试使用此代码。

<html>
<body>

<?php
echo "<table style='border: solid 1px black;'>";
 echo "<tr><th>Name</th><th>Email</th><th>Content</th><th>Date</th><th>Amount</th></tr>";

class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }

    function current() {
        return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() { 
        echo "<tr>"; 
    } 

    function endChildren() { 
        echo "</tr>" . "\n";
    } 
} 

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "order";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("SELECT * FROM ordersum"); 
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
        echo $v;
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?> 

</body>
</html>

<?php

$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"order");
$res=mysqli_query($link,"select * from ordersum");

?>

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM