Get image from MYSQL database

Hi,

I'm doing php coding and would like to know how to get images from MYSQL database.
I inserted a field( flag ) with BLOB data-type in countries table ( catid, catname, flag) but having problem fetching those images from DB. I tried to fetch but it gave me meaningless representation like this:
HDR\r¨f	pHYsšœ OiCCPPhotoshop ICC profilexڝSgTSé=÷ÞôBKˆ€”KoR RB‹€‘&*!	Jˆ!¡ÙQÁEEÈ ˆŽŽ€ŒQ,Š Øä!¢Žƒ£ˆŠÊûá{£kÖ¼÷æÍþµ×>ç¬ó³ÏÀ–H3Q5€©BàƒÇÄÆáä.@ $p³d!sý#ø~<<+"À¾xÓÀM›À0‡ÿêB™\€„Àt‘8K€@zŽB¦@F€˜&S `ËcbãP-`'æÓ€ø™

Open in new window


Here are the functions that I'm working on:
function get_countries() {
   // query database for a list of countries
   $conn = db_connect();
   $query = "select catid, catname, flag from countries";
   $result = @$conn->query($query);
   if (!$result) {
     return false;
   }
   $num_cats = @$result->num_rows;
   if ($num_cats == 0) {
      return false;
   }
   $result = db_result_to_array($result);
   return $result;
}

Open in new window


function display_countries($cat_array) {
  if (!is_array($cat_array)) {
     echo "<p>No countries currently available</p>";
     return;
  }
  echo "<ul>";
  foreach ($cat_array as $row)  {
    $url = "show_cat.php?catid=".$row['catid'];
    $title = $row['catname'].$row['flag'];
    echo "<li>";
    do_html_url($url, $title);
    echo "</li>";
  }
  echo "</ul>";
  echo "<hr />";
}

Open in new window

hmbargiAsked:
Who is Participating?
 
Lukasz ChmielewskiCommented:
Try to tell the browser it is an image with a line preceding to the fetched image row:

header("content-type: image/jpg") ;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.