View Image in browser

//DB Connection

$sql ="select img_val from image where id=152";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    $Image=$row['img_val'];
#       print "Image is $Image";
}

header( "Content-type: image/jpeg");
echo "<img src=\\"$Image\\"; alt=\\"Image from DB\\" />";


When I view the file in browser, some characters are printed and not the image.
Please tell me how to view the exact image in the browser.


Array ( [0] => Image Object ( [Key:protected] => Key Object ( [Id:protected] => 19 [name:protected] => image [reportable:protected] => [value:protected] => &#65533;&#65533;&#65533;&#65533;&#65533;JFIF&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;XPhotoshop 3.0&#65533;8BIM&#65533;&#65533;&#65533;&#65533;<x6RINGING IN THE SEASON - The La Ca&#65533;ada Towne singers, under the direction of Sean Boulware, will be performing its annual holiday concert on Dec. 4 and 5 in Lanterman Auditorium. The group, with the help of some dancing reindeer, will take a comical look at the trials and tribulations of holiday shopping.&#65533;&#65533;&#65533;&#65533;&#65533;&File written by Adobe Photoshop&#65533; 5.2&#65533;&#65533;&#65533;C&#65533;aaaa  $.' ",#(7),01444'9=82<.342&#65533;&#65533;&#65533;C 2!!22222222222222222222222222222222222222222222222222&#65533;&#65533;&#65533;&#65533;o&#65533;&#65533;"&#65533;&#65533;&#65533;&#65533;e&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;a&#65533;&#65533;&#65533;B&#65533;a&#65533;&#65533;&#65533;&#65533;!1AQ"aq&#65533;&#65533;&#65533;2&#65533;$B&#65533;&#65533;#3RbaCSr&#65533;&#65533;&#65533;&#65533;%cu&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;?&#65533;&#65533;&#65533;&#65533;h&#65533;&#65533;4f&#65533;&#65533;hh&#65533;*(h&#65533;*(:&#65533;,&#65533;E&#611;&#65533;TP<&#65533;Z2iQ@&#65533; Z2iQ@&#65533;}id&#65533;&#1186;&#65533;&#65533;&#65533;Z[&#65533;&#65533;*F&#65533;&#65533;&#65533;&#1421;&#65533;w5&#65533;&#65533;&#65533;1#&#65533;*1&#1418;&#65533;i&#65533;;&#65533;*x&#65533;&#65533;&#65533;E&#35274;E:1@&#65533;&#65533;F( &#65533;TS&#65533;&#65533;TS&#65533;*t&#65533;&#65533;P&#65533;&#65533; &#65533;&#65533;&#24610;&#65533;(&#1418;aZ(5"&#65533;&#65533;Ek&#65533;.$o*&#65533;*q&#1262;k>&#65533;y&#65533;&#65533;t&#65533;0U&#65533;&#65533; &#65533;F&#65533;gy&#65533;j&#65533;gl7( &#65533;da&#65533;s&#65533;&#65533;&#65533;=7>"&#65533;&#65533;&#65533;D&#65533;(;N &#65533;&#65533;&#65533;&#65533;&#65533;$H&#65533;g&#65533;&#65533;B&#65533;(H&#65533;&#65533;&#905;%&#65533;&#65533;&#65533;V&#65533;w&#65533;*&#65533;}&#65533;&#65533;&#65533;&#65533;W&#65533;]r&#65533;&#65533;&#65533;=&#65533;&#65533;V&#65533;&#65533;&#1912;dw&#65533;&#1446;&#65533;t&#65533;-4]2Y.&#65533;&#1901;dJ?&#65533;$&#65533;&#65533;A&#65533;&#65533;&#65533;A&#65533;g&#65533;\\&#65533;&#1788;&#65533;&#65533;m&#65533;&#65533;&#65533;*eke&#65533;&#65533;Z;h$&#65533;&#65533;e&#65533;p+g&#65533;q&#65533;_y&#65533;|S&#65533; G&#65533;&#65533;<&#65533;&#65533;^&#65533;&#65533;>&#65533;R9&&#65533;&#65533;

When I query the table and print the values for image, I get the array outputted this way.
I have to extract only output of “value” using the separate function.

I am not understanding what is the protected which is printed out.

One more thing, why are you sing while loop for a particular single record? Or you supposed to be retrieved multiple records from the ID=152?


//DB Connection
$sql ="select img_val from image where id=152";
$result = mysql_query($sql) or die(mysql_error());
if(count($result) >= 1){ // check if the record found
    $row = mysql_fetch_assoc($result);
    $Image = $row['img_val'];
    
    if(!empty($Image)){ // check if the field has some data
        // print image
        header( "Content-type: image/jpeg");
        echo $Image;
    }
}

If you just store the image data in the table it will be stored as binary data in the table and you may not be able to find out the mime type for the binary data. So while uploading or storing the image data in the table, you should find out what type of image you are storing and store it in a separate field in the table so that you can use it later.

But I would suggest you to use file system rather than to store the image data in the database table. I hope you understand how file system is done. Store the images in some folders/directories in the server and just store the file path/image names in the database.

In the DB image is stored. How to get the image headers from the Db value.

The output should look like

Array ( [0] => HTTP/1.1 200 OK [1] => Date: Mon, 07 Jun 2010 06:40:46 GMT [2] => Server: Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8l mod_onsint/1.0 [3] => Last-Modified: Wed, 19 May 2010 19:18:51 GMT [4] => ETag: "728dbb-6fd0-53fd5cc0" [5] => Accept-Ranges: bytes [6] => Content-Length: 2862 [7] => Content-Type: image/jpeg [8] => [9] => )

Can you post your whole code which produced the above array output for you?

Assuming $image has the binary data:


//DB Connection

$sql ="select img_val from image where id=152";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    $Image=$row['img_val'];
#       print "Image is $Image";
}

header( "Content-type: image/jpeg");
echo $Image;

Function is called here


 $results = ConImage::getAllById((int)$Id);

Filename ConImage.php.
But i do not want to change the function
getAllById


   function getAllById($Id)
  {

    $link = db_connect();
    $qry = "
SELECT id
FROM image
WHERE id = $Id
";
    $result = mysql_query($qry, $link) or debug_die($qry, $link);
    $all = array();
    while ($row = mysql_fetch_assoc($result)) {
      $anInst = new ConImage($row['id']);
      $all[] = $anInst;
    }
    mysql_free_result($result);
    return $all;
  }