如何以表格形式显示我的数据库。这是我的代码:
<?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>";
}
?>
答案 0 :(得分:0)
试试这个,希望可以帮到你。
<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>
答案 1 :(得分:0)
您的代码应如下所示:
<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,for,foreach)。 / p>
答案 2 :(得分:0)
我尝试使用此代码。
<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");
?>