What is the command to retrieve all field names from a table in PHP?
To retrieve all field names from a table in PHP, you can use the `SHOW COLUMNS` query in MySQL. This query will return information about the columns in a table, including the column names. You can then fetch the results and extract the column names from the returned data.
<?php
// Connect to the database
$conn = new mysqli('localhost', 'username', 'password', 'database');
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to retrieve all field names from a table
$sql = "SHOW COLUMNS FROM your_table_name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['Field'] . "<br>";
}
} else {
echo "0 results";
}
// Close connection
$conn->close();
?>