How can PHP be used to fetch and display data from a database outside of a WordPress environment?

To fetch and display data from a database outside of a WordPress environment using PHP, you can establish a connection to the database using PDO or MySQLi, query the database to retrieve the desired data, and then display it on the webpage using PHP echo or print statements.

<?php
// Establish database connection using PDO
$host = 'localhost';
$dbname = 'database_name';
$username = 'username';
$password = 'password';

try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // Query the database to fetch data
    $stmt = $conn->query("SELECT * FROM table_name");
    $result = $stmt->fetchAll();

    // Display the fetched data
    foreach ($result as $row) {
        echo $row['column_name'] . "<br>";
    }

} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>