What are the potential pitfalls of directly accessing and displaying binary data from a database in PHP?

Directly accessing and displaying binary data from a database in PHP can pose security risks such as SQL injection attacks if the data is not properly sanitized. To mitigate this risk, it is important to use prepared statements and parameterized queries when retrieving and displaying binary data.

// Example of using prepared statements to safely retrieve and display binary data from a database
$stmt = $pdo->prepare("SELECT image_data FROM images WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);

// Display the binary data as an image
header("Content-type: image/jpeg");
echo $row['image_data'];