How can PHP be used to display data from a database on an external HTML page?

To display data from a database on an external HTML page using PHP, you can establish a database connection, query the database for the desired data, and then output the data within the HTML page using PHP echo statements.

<?php
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);

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

// Query the database for data
$sql = "SELECT column1, column2 FROM table_name";
$result = $conn->query($sql);

// Output the data within the HTML page
echo "<html>";
echo "<body>";
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Column 1: " . $row["column1"]. " - Column 2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}
echo "</body>";
echo "</html>";

// Close the database connection
$conn->close();
?>