我想從mariadb數據庫創建一個html表。我不知道每個數據庫表將以多少列結束。php創建數據庫中具有未知數量列的html表
所以我不知道我有多少列有,我想出了這個:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles/global.css">
</head>
<body>
<?php
$con=mysqli_connect("localhost","root","","test");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//query
$sql="SELECT * FROM news ORDER BY author";
$result=mysqli_query($con,$sql);
$counter = 0;
while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo $counter;
echo nl2br("\n");
?>
<table>
<thead>
<?php
if($counter==0){ ?>
<tr>
<th><?php echo implode("</th><th>", array_keys($row)); ?>
</th>
</tr>
}
</thead>
<tbody>
<?php
if($counter>=0){ ?>
<tr>
<td><?php echo implode("</td><td>", $row); ?>
</td>
</tr>
}
</tbody>
</table>
<?php
$counter ++;
}
mysqli_free_result($result);
mysqli_close($con);
?>
</body>
</html>
的問題是,該表不會比第一行多補。
另外我有得到錯誤
Parse error: syntax error, unexpected end of file in E:\xampp\htdocs\test.php on line 58 Blockquote
當我輸入兩個大括號錯誤消失 「}}」 while循環後,會出現以下內容:
我不知道爲什麼在圖片中看到瀏覽器中的花括號。
編輯:我編輯線程,因爲它被標記爲parsing and syntax errors重複,雖然我的主要問題是創建具有未知列數的表。
任何想法如何解決這個問題?感謝你們!
首先將你想要重複的東西重複多次INSIDE while while循環 – RiggsFolly
首先分離你的代碼和你的HTML,然後正確縮進。然後你將很容易發現你的錯字。 – miken32
是的,謝謝你,我總是試圖在一個大循環內做一切事情,需要在那個想法上練習。 –