How can the header() function be properly utilized in conjunction with mysql_fetch_assoc() to display images stored in a database?

When displaying images stored in a database using mysql_fetch_assoc(), the header() function should be used to set the content type of the image before outputting it. This ensures that the browser interprets the data as an image and displays it correctly. By setting the content type to "image/jpeg" or "image/png" based on the image type stored in the database, the image can be displayed properly.

<?php
// Assume $row is the result fetched using mysql_fetch_assoc()
$image = $row['image_data']; // Assuming 'image_data' is the column name where image is stored
$image_type = $row['image_type']; // Assuming 'image_type' is the column name where image type is stored

header("Content-type: image/$image_type");
echo $image;
?>