retrieving and displaying image from mysql. db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zobie
Forum Newbie
Posts: 1
Joined: Fri Jun 23, 2006 4:40 pm

retrieving and displaying image from mysql. db

Post by zobie »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey guys,

I've been trying to display some images stored in a mysql database. The upload script seems to work fine. My problem arises when i try to display the images.

I use two files, one called view.php and the other view_image.php.

The view.php file includes the following code.

Code: Select all

$query = "SELECT mime_type,image FROM testblob2 
            WHERE id = 1";

//the image field is of type longblob and the mime_type field is of type varchar

  $result = @ mysql_query ($query,$connection);

  $data = @ mysql_fetch_array($result);

  if (!empty($data["image"]))
  { 
    // Output the MIME header
    // header("Content-Type: {$data["mime_type"]}");
    // Output the image
     echo $data["image"];
   }
When i pulled this script (view.php) through the browser i received the same output that you get when you open up an image file with a text editor. A bunch of weird symbols.

I then included the following code in the view_image.php file:

Code: Select all

$query = "SELECT id,image_name FROM testblob2";

$result = @mysql_query($query,$connection);	
if($row = @mysql_fetch_array($result))
{
             echo "{$row["image_name"]}";
	echo "<img src=\"view.php?file={$row["id"]}\">";
}

When i pull up view_image.php through the web browser i get a broken link where the image is supposed to be. I can't seem to resolve this issue.

I also would like to fully understand the line

Code: Select all

echo "<img src=\"view.php?file={$row["id"]}\">";
Where does the word "file" come from since i can't find a line where it gets passed to the script and what is the function of the "?"

I guess its similar to the url above http://www.sitepoint.com/forums/newthre ... hread&f=34

why is the word or value "do=newthread" used and where does it come from. Does it have to be passed to the script using GET or POST .

I'm just trying to pinpoint where i've gone wrong. I'm a relative newbie to php coding but i don't believe that retrieving and displaying an image from a database should be so frustrating. Must be something simple that i'm missing.

Another forum that i checked mentioned that a broken link for the image usually means that the header has not be sent. Should I be sending the filesize with the header as well .

I'm not debating whether or not I should have stored the absolute paths instead of storing the images in the databse as blobs. I'd just like to deal with the issue at hand. Displaying the images that are currently stored.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: retrieving and displaying image from mysql. db

Post by RobertGonzalez »

zobie wrote:When i pulled this script (view.php) through the browser i received the same output that you get when you open up an image file with a text editor. A bunch of weird symbols.
I think you need to use the header() function to tell the script that you are about to display an image...
zobie wrote:When i pull up view_image.php through the web browser i get a broken link where the image is supposed to be. I can't seem to resolve this issue.
That because using the query string (file=something) implies that you are telling view.php which picture to get. But your code in view.php does not do that. Although, since it is pulling id 1 from the table, if view.php actually shows anything, then the link you have should render a picture.
zobie wrote:I also would like to fully understand the line

Code: Select all

echo "<img src="view.php?file={$row["id"]}">";
Where does the word "file" come from since i can't find a line where it gets passed to the script and what is the function of the "?"

I guess its similar to the url above http://www.sitepoint.com/forums/newthre ... hread&f=34

why is the word or value "do=newthread" used and where does it come from. Does it have to be passed to the script using GET or POST.
You would insert the 'file' query string var in your code so that way the page that is referencing the query string var knows what data is being passed to it by $_GET.
Post Reply