What are the best practices for handling dynamic links from a database when including files in PHP?

When including files in PHP that contain dynamic links from a database, it is important to sanitize the input to prevent SQL injection attacks and ensure that the links are properly formatted. One way to handle this is to retrieve the dynamic links from the database, sanitize them using a function like mysqli_real_escape_string, and then use them in your include statement.

// Retrieve dynamic link from database
$link = mysqli_real_escape_string($conn, $_GET['link']);

// Include file with dynamic link
include 'path/to/files/' . $link;