What are some best practices for creating dynamic links from database entries in PHP?

When creating dynamic links from database entries in PHP, it is important to properly sanitize the data to prevent SQL injection attacks. One way to achieve this is by using prepared statements with parameterized queries. Additionally, make sure to properly escape any user input before using it in the link.

// Assuming $row is an associative array containing the database entry
$id = $row['id'];
$title = htmlspecialchars($row['title']); // Sanitize user input
$url = "http://example.com/page.php?id=" . $id;

echo '<a href="' . $url . '">' . $title . '</a>';