What is the best way to retrieve information about fields in a MySQL table using PHP?
To retrieve information about fields in a MySQL table using PHP, you can use the "DESCRIBE" SQL statement to get the details of the table structure. This statement provides information about the columns in the table, including the field name, data type, and any additional attributes.
<?php
// Connect to the 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);
}
// Get table structure
$table_name = "your_table_name";
$sql = "DESCRIBE $table_name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Field: " . $row['Field'] . " | Type: " . $row['Type'] . "<br>";
}
} else {
echo "Table not found";
}
$conn->close();
?>