我想从 mariadb 数据库创建一个 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>
问题是表格不会填充超过第一行。
另外我得到了错误
解析错误:语法错误,E:\xampp\htdocs\test.php 中第 58 行的文件意外结束 Blockquote
当我在while循环后输入两个大括号“}}”时,错误消失,它将显示以下内容:
我不知道为什么浏览器中有大括号,如图所示。
编辑:我编辑线程,因为它被标记为重复 解析和语法错误,尽管我的主要问题是创建列数未知的表。
知道如何解决这个问题吗?感谢你们!