What are some common pitfalls when trying to create hyperlinks in PHP based on database query results?

One common pitfall when creating hyperlinks in PHP based on database query results is not properly escaping the data retrieved from the database, which can lead to security vulnerabilities like SQL injection. To solve this issue, it's important to use prepared statements or escape the data before outputting it in the hyperlink.

// Assuming $result is the database query result containing the data for hyperlinks
while($row = $result->fetch_assoc()) {
    $escapedData = htmlspecialchars($row['data']); // Escape the data
    echo "<a href='page.php?data=" . $escapedData . "'>" . $escapedData . "</a><br>";
}