Page 1 of 1

Image Retrieving Problem

Posted: Mon Mar 07, 2011 1:08 am
by maanikmca
Hi,

am new PHP Programmer, here i want display the image from database, i can store the image into database,
but i cant retrieve the image, while am googling for that i got piece of code, but i unable to process it ..it show me just 1 URL
cant display the image.

Here i the code

Code: Select all

<?php

  // just so we know it is broken
 error_reporting(E_ALL);

    // some basic sanity checks
  if(isset($_POST['image_id']) && is_numeric($_POST['image_id'])) {

        //connect to the db
             $link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());

      // select our database
        mysql_select_db("database1") or die(mysql_error());

        // get the image from the db
         $sql = "SELECT image FROM testblob WHERE imageid=1";

        // the result of the query
          $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

        // set the header for the image
      // 

		
		while ($row = mysql_fetch_array($result))
{

header("Content-type: image/jpeg");
         print $row['image'];
  
     }
       // echo mysql_result($result, 0);
 
        // close the db link
        mysql_close($link);
    }
    else {
        echo 'Please use a real id number';
    }

		
		
		
?>
when i code this code into the php

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

it shows me just one link

http://localhost/mar4task/shoppingcart1/imagedb.php..


please sort out my issue ?

Re: Image Retrieving Problem

Posted: Mon Mar 07, 2011 6:12 pm
by mecha_godzilla
Have you tried echo()ing out the value contained in $row['image'] to see whether it's in the right format - I usually echo() out a pair of <pre></pre> tags before and after the data I'm outputting to make sure it's in the right format but you'll need to remove the header() tag that you've got in there to do this.

An alternative way to test this is to output your image to a file - to do this just use

Code: Select all

$file_pointer = fopen('my_image.jpg', 'wb')
to make sure the file is written to in binary format. You can then access it directly in your browser window to see whether it's a valid image or not.

HTH,

Mecha Godzilla