How can I display only the column names from a database using PHP?
To display only the column names from a database using PHP, you can query the information_schema database to retrieve the column names for a specific table. You can then loop through the results and display only the column names.
<?php
// Connect to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query the information_schema database to get column names
$sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$dbname' AND TABLE_NAME = 'your_table_name'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output column names
while($row = $result->fetch_assoc()) {
echo $row["COLUMN_NAME"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- What are some best practices for accessing and manipulating stdClass objects in PHP?
- How can PHP developers optimize database queries to ensure accurate and efficient results when sending emails to specific recipients based on workshop participation?
- How can PHP developers ensure the security and integrity of user data when using session variables in database operations?