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;
?>
Keywords
Related Questions
- How can PHP classes be effectively used to handle database connections and queries in a modular project?
- What are the advantages of using superglobals like $_POST instead of $HTTP_POST_VARS in PHP when handling form data?
- What are the advantages and disadvantages of using Dreamweaver for generating code compared to hand-coding in PHP?