0
您好,我是一名帶有html和php和mysql的新程序員。實際上,我想從mysql中的唯一表中選擇所有數據,並在html5頁面上顯示它們。 我創建了一個sms.html,當我使用Chrome運行它,這是我所看到的Normally I need to display a table with 5 th means 5 rows如何從mysqltable中選擇所有數據並在html上顯示它們
這裏是我的代碼
<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>
這是不重複的,至少不是從http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-頁面 –
如何增加表格的大小,因爲我們的表格太小 –