Sometimes, we want to use MySQL query to get column names with PHP.
In this article, we’ll look at how to use MySQL query to get column names with PHP.
How to use MySQL query to get column names with PHP?
To use MySQL query to get column names with PHP, we can use the SHOW COLUMNS FROM your-table
query.
For instance, we write
$sql = "SHOW COLUMNS FROM your-table";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($result)) {
echo $row['Field'] . "<br>";
}
to call mysqli_query
with the $conn
connection object and the $sql
string.
Then we have a while loop to fetch the results with mysqli_fetch_array
.
In the loop, we get the field name with $row['field']
.
We place your-table with the name of the table we’re querying for.
Conclusion
To use MySQL query to get column names with PHP, we can use the SHOW COLUMNS FROM your-table
query.