How can PHP beginners effectively use the DESCRIBE function to get specific information from a SQL table?

When using the DESCRIBE function in PHP to get specific information from a SQL table, beginners can effectively retrieve details about the columns, data types, and other properties of the table. By executing a SQL query with DESCRIBE, users can easily obtain the information they need to understand the structure of the table and make informed decisions about their database operations.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Get table structure information using DESCRIBE
$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 "0 results";
}

$conn->close();
?>