显示mysql表而不知道PHP中的cols总数

I need to display mysql table columns without specifying column names in code and I also don't know total number of columns in the table.

Here is my code:

$result = mysqli_query($con,"SELECT * FROM test_table");
echo "<table border='1'>";
while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    foreach ($row as $item){
        echo "<td>" . $item . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

But output is as follows:

enter image description here

i.e values are repeating for each col. Please help me out.

This is because mysqli_fetch_array fetches both an associative array AND a numeric array. Try using mysqli_fetch_assoc or mysqli_fetch_row.

Alternatively, you can specify a parameter in mysqli_fetch_array, like so:

mysqli_fetch_array($result, MYSQLI_ASSOC)

Or

mysqli_fetch_array($result, MYSQLI_NUM)

Change

while($row = mysqli_fetch_array($result))

To

while($row = mysqli_fetch_row($result))