In the context of PHP, what are some best practices for organizing and displaying data from a database?

When organizing and displaying data from a database in PHP, it is important to separate your database logic from your presentation logic. One common best practice is to use a separate file for your database connection and queries, and then use a separate file for displaying the data in a user-friendly format. This separation of concerns helps to keep your code clean and maintainable.

// database.php
<?php
$host = 'localhost';
$dbname = 'my_database';
$username = 'root';
$password = '';

try {
    $db = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

// display_data.php
<?php
include 'database.php';

$stmt = $db->query('SELECT * FROM my_table');
$data = $stmt->fetchAll();

foreach($data as $row) {
    echo $row['column1'] . ' - ' . $row['column2'] . '<br>';
}