In PHP, what are the best practices for integrating database content into different pages of a website and ensuring correct display based on URLs?

When integrating database content into different pages of a website, it's important to use PHP to fetch the data based on the URL parameters or page identifiers. One approach is to use a PHP script that receives the page identifier from the URL, queries the database for the relevant content, and then displays it on the page. This ensures that the correct content is displayed based on the URL, providing a dynamic and personalized experience for users.

<?php
// Assuming a database connection is already established

// Get the page identifier from the URL
$page_id = $_GET['page_id'];

// Query the database for content based on the page identifier
$query = "SELECT * FROM pages WHERE id = $page_id";
$result = mysqli_query($connection, $query);

// Display the content on the page
if(mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    echo "<h1>{$row['title']}</h1>";
    echo "<p>{$row['content']}</p>";
} else {
    echo "Page not found";
}
?>