Skip to content
Advertisement

How do I show only the column name of the SQL table [closed]

Currently my code consists of

$sql = "SHOW columns FROM tblexercise";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
  echo "<tr>";
  foreach ($row as $field => $value) {

      echo "<td>" . $value . "</td>";
  }
  echo "</tr>";
}

This allows me to show the column names but it also includes all the attributes and types etc. is there any way to show just the column name?

Advertisement

Answer

You don’t need a loop inside a loop. You only need one foreach loop and then you can access the key Field which holds the name of the column. The query SHOW COLUMNS is explained in the MySQL doc. You can check in that link what are the results of this query and their sample values. Then you can decide which values you want to access.

$result = $conn->query("SHOW columns FROM tblexercise");
foreach ($result as $row) {
    echo "<tr>";
    echo "<td>" . $row['Field'] . "</td>";
    echo "</tr>";
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement