gpt4 book ai didi

javascript - 如何从 mysqltable 中选择所有数据并将其显示在 html 上

转载 作者:行者123 更新时间:2023-11-29 21:04:26 24 4
gpt4 key购买 nike

你好,我是一名新程序员,擅长 html、php 和 mysql。事实上,我想从 mysql 中的唯一表中选择所有数据并将它们显示在 html5 页面上。我创建了一个 sms.html,当我用 Chrome 运行它时,我看到的是 Normally I need to display a table with 5 th means 5 rows

这是我的代码

    <body>
<?php
echo "<table style='border: solid 1px black; border-collapse: collapse;
th, td {
padding: 5px;
text-align: left;
}'>";
echo "<tr>
<th>date/th>
<th>Etat de TX1</th>
<th>Etat de TX2</th>
<th>Sur antenne</th>
<th>ALARME</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 = "OACA";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,

$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM etat");
$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>

最佳答案

你能不能别做这样的事?

 <?php
`$result = null;

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

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare("SELECT * FROM etat");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$count = $stmt -> rowCount();
$all = $stmt->fetchALL();
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}

?>

<div>
<table style='border: solid 1px black; border-collapse: collapse;
th, td {
padding: 5px;
text-align: left;
}'>
<thead>
<tr>
<th>date/th>
<th>Etat de TX1</th>
<th>Etat de TX2</th>
<th>Sur antenne</th>
<th>ALARME</th>
</tr>
</thead>
<tbody>
<?php
if($count > 0) {
//For each row echo <tr></tr>
foreach ($all as $item) {
echo"<tr>";
//For each column in the row echo each value in a <td></td>
foreach ($item as $key => $value) {
echo "<td>" . $value ."</td>";
}
"</tr>";
}

}
?>
</tbody>

</table>
</div>

关于javascript - 如何从 mysqltable 中选择所有数据并将其显示在 html 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36949475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com