动态回显所有列名和所有行

echo all coloumn name and all rows dynamically

兄弟们,我正在尝试回显 table 中的所有数据,包括列名和字段。我能够成功获取列名,我还想获取所有的行和字段,我不确定我是否采取了正确的方法来解决我的问题,请帮助。

//this is to echo coloumn names
$table = '<table border="1" cellspacing="10" cellpadding="10" style="width:60%">';
$table2 = "</table>";
$sql2 = "SHOW COLUMNS FROM $tablename";
$query2 = mysqli_query($conn,$sql2);
$tablename = $_POST['db'];
echo $table;
while($row = mysqli_fetch_array($query2)){
echo $row[0] . "&nbsp;&nbsp;&nbsp;";
}
echo $table2 . "<br /><br />";
// successful in echoing coloumn names

// now i want to echo rows based on the number of coloumns
$countcol = mysqli_num_rows($query2);
$sql = "SELECT * FROM $tablename";
$query = mysqli_query($conn,$sql);

echo $table;
$i = "";
while($row=mysqli_fetch_array($query)){
for($i=0;<$countcol;$i++){
echo $row[0];
}
}
echo $table2 . "<br /><br />";
// this doenst get the all the rows based on the no of coloumns

在最后一个循环中,echo $row[0] 是正确的还是应该是 $row[$i]