What potential pitfalls should be considered when outputting image links from a database in PHP?

When outputting image links from a database in PHP, it is important to consider potential security vulnerabilities such as SQL injection and Cross-Site Scripting (XSS) attacks. To mitigate these risks, it is recommended to sanitize the image links before outputting them to the browser. This can be done by using PHP's htmlspecialchars() function to escape special characters and prevent malicious code from being executed.

<?php
// Retrieve image link from the database
$imageLink = $row['image_link'];

// Sanitize the image link before outputting
$sanitizedImageLink = htmlspecialchars($imageLink);

// Output the sanitized image link
echo "<img src='" . $sanitizedImageLink . "' alt='Image'>";
?>