12 Replies - 109369 Views - Last Post: 25 August 2012 - 02:26 PM Rate Topic: -----

#1 BoneXXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-September 07

Display Image From MySQL Database

Posted 03 May 2008 - 03:45 AM

Hi all,

The problem I am having is when I call an image from a MySQL database, rather than getting the image I get thousands lines of gibberish characters.

I placed the

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


at the top of the page but now I just get a blank white page with the URL address on it?Could you help me on this please?
Is This A Good Question/Topic? 0
  • +

Replies To: Display Image From MySQL Database

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: Display Image From MySQL Database

Posted 03 May 2008 - 11:01 AM

First of all, storing images directly into a database is bad. A big no no. This causes a ton of problems from bloating your database to things like having to fetch the graphic back out.

The solution however is to create a PHP script which will take the id of the record with the picture, fetch it from the database, display it with the header you have there, then include a call to this page (using the id) in an image tag.

An example would be like the one below...

<?php
mysql_connect("localhost","wellho","xxxxxxx");
mysql_select_db("wellho");
$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("select * from im_library where filename=\"".
addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];
header("Content-type: image/jpeg");
print $imagebytes;
?>



This would be the page for accepting the filename (or you could use an id which might be better) and printing it. Then you would save this page in a page called something like "myimage.php" and call it from your other page using an img tag like so...

<img src="myimage.php?imname=apic">



So when the browser runs across this img tag, it executes the script which prints the image and returns it to be placed in the img tag.

Hope that helps you out. And in the future, always store a path to the file and not the actual file itself in the database.

:)
Was This Post Helpful? 0
  • +
  • -

#3 BoneXXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-September 07

Re: Display Image From MySQL Database

Posted 03 May 2008 - 10:29 PM

Thank you for teh replay, but unfortunatly didin't work. So I wanted the see how the script work by itself i just created a page to get the image from tha database and puts on the screen.

<?php

//include ("../databaseConnect/dbfuncs.inc.php");
//$link = connectToDatabase();
mysql_connect("localhost","eventman","farside");
mysql_select_db("eventman");
$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("select * from Logo where ImageTitle=\"Utas_vert".addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];
//header("Content-type: image/jpeg");
print $imagebytes;
echo "<img src='imagedisp.php?imname=Utas_vert.jpg'>";
?>



I just get a blank page with a imagespot which can't be displayed. If i don't comment the header I get balnk page with a URL address of the page(not link). Can you see anything wrong in this code?Thanks
------------------------------------------------------------------------------------------------------------------------------------

View PostMartyr2, on 3 May, 2008 - 11:01 AM, said:

First of all, storing images directly into a database is bad. A big no no. This causes a ton of problems from bloating your database to things like having to fetch the graphic back out.

The solution however is to create a PHP script which will take the id of the record with the picture, fetch it from the database, display it with the header you have there, then include a call to this page (using the id) in an image tag.

An example would be like the one below...

<?php
mysql_connect("localhost","wellho","xxxxxxx");
mysql_select_db("wellho");
$image = stripslashes($_REQUEST[imname]);
$rs = mysql_query("select * from im_library where filename=\"".
addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[imgdata];
header("Content-type: image/jpeg");
print $imagebytes;
?>



This would be the page for accepting the filename (or you could use an id which might be better) and printing it. Then you would save this page in a page called something like "myimage.php" and call it from your other page using an img tag like so...

<img src="myimage.php?imname=apic">



So when the browser runs across this img tag, it executes the script which prints the image and returns it to be placed in the img tag.

Hope that helps you out. And in the future, always store a path to the file and not the actual file itself in the database.

:)

This post has been edited by BoneXXX: 03 May 2008 - 10:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 BoneXXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-September 07

Re: Display Image From MySQL Database

Posted 03 May 2008 - 11:02 PM

I am able to display the image with this code
<?php 

mysql_connect("localhost","xxxxxx","xxxxxx");
mysql_select_db("xxxxxx");
$query_category="SELECT ImageContent FROM Logo";
$result_category = mysql_query($query_category);
while($row=mysql_fetch_row($result_category))
{
	header("Content-type: image/jpeg");
	echo "$row[0]";
}	
?>





How can I use this to display the image the way you specified?Thanks

This post has been edited by BoneXXX: 03 May 2008 - 11:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Display Image From MySQL Database

Posted 03 May 2008 - 11:12 PM

As Martyr2 stated it is indeed a bad idea to store images in a database, but since you've already made the decision to do so, lets take a look at accomplishing the displaying of the image. First, you are storing it a BLOB data type correct?

Here we go:


//make sure there is an image name provided
if(isset($_REQUEST[imname])
{
        //connect to the db
        $db = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());

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

        //get the image name we're after
        $image = stripslashes($_REQUEST[imname]);

        //build our query
        //NEVER use select *
        $sql_query = "select image from Logo where ImageTitle=\"Utas_vert".addslashes($image).".jpg\"";

        //get the results of the query
        $query_results = mysql_query("$sql_query") or die("Invalid query: " . mysql_error());

        // set the header for the image
        header("Content-type: image/jpeg");
        echo '<img  src="'.mysql_result($query_results, 0).'">';
        // close the db link
        mysql_close($db);
}
else
{
        echo "Image name not provided!";
}




Try that and see if it works for you :)
Was This Post Helpful? 0
  • +
  • -

#6 BoneXXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-September 07

Re: Display Image From MySQL Database

Posted 03 May 2008 - 11:43 PM

Thank you for the reply.
		//connect to the db
		$db = mysql_connect("localhost", "eventman", "farside") or die("Could not connect: " . mysql_error());

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

		//get the image name we're after
		$image = stripslashes($_REQUEST[imname]);

		//build our query
		//NEVER use select *
		$sql_query = "select ImageContent from Logo where ImageTitle=\"Utas_vert".addslashes($image).".jpg\"";

		//get the results of the query
		$query_results = mysql_query("$sql_query") or die("Invalid query: " . mysql_error());

		// set the header for the image
		header("Content-type: image/jpeg");
		echo '<img  src="'.mysql_result($query_results, 0).'">';
		// close the db link
		mysql_close($db);



And call it from a diffrent page:
echo "<img src='imagedisp.php?imname=Utas_vert'>";



And the output was just a blank page with text with the URL address(http://URL_ADDRESS/imagedisp.php)
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Display Image From MySQL Database

Posted 03 May 2008 - 11:54 PM

If you look at the code I provided I dont call it from another page, I output the value right onto the page. This is just to show you how to display the image
Was This Post Helpful? 0
  • +
  • -

#8 BoneXXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-September 07

Re: Display Image From MySQL Database

Posted 04 May 2008 - 03:04 AM

Ok. This is exactly what I did:

Imagedisp.php
<?
        //connect to the db
        $db = mysql_connect("localhost", "eventman", "farside") or die("Could not connect: " . mysql_error());

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

        //get the image name we're after
        $image = stripslashes($_REQUEST[imname]);

        //build our query
        //NEVER use select *
        $sql_query = "select ImageContent from Logo where ImageTitle=\"Utas_vert".addslashes($image).".jpg\"";

        //get the results of the query
        $query_results = mysql_query("$sql_query") or die("Invalid query: " . mysql_error());

        // set the header for the image
        header("Content-type: image/jpeg");
        echo '<img  src="'.mysql_result($query_results, 0).'">';
        // close the db link
        mysql_close($db);
?>



This page outputs blank page with URL address text on it "http://xxxxx.com/imagedisp.php".
Is there anything wrong in these codes? Thanks

View PostPsychoCoder, on 3 May, 2008 - 11:54 PM, said:

If you look at the code I provided I dont call it from another page, I output the value right onto the page. This is just to show you how to display the image

This post has been edited by BoneXXX: 04 May 2008 - 04:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: Display Image From MySQL Database

Posted 04 May 2008 - 09:57 AM

//get the image name we're after
$image = stripslashes($_REQUEST[imname]);

//build our query
//NEVER use select *
$sql_query = "select ImageContent from Logo where ImageTitle=\"Utas_vert".addslashes($image).".jpg\"";



You do realize that the code above is going to result in a filename like ImageTitle="Utas_vert<imagename.jpg>.jpg" right? I am not sure how you have the ImageTitle stored in your database, but I recommend that you echo out your query and make sure that the ImageTitle you are specifying is correct. Something tells me you are not building the file name correctly. If you are going to add the "jpg" extension onto the filename in the code, you can't specify the extension when you pass the file name through the <img> tag.

:)
Was This Post Helpful? 0
  • +
  • -

#10 BoneXXX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 26-September 07

Re: Display Image From MySQL Database

Posted 11 May 2008 - 09:51 PM

Thank you for the replies.

I just get the first row's image with this code. Shouldn't it display the images of all the rows?

<?php 
mysql_connect("localhost","xxxxxx","xxxxxx");
mysql_select_db("xxxxxxxx");
$query_category="SELECT ImageContent FROM Logo";
$result_category = mysql_query($query_category);
while($row=mysql_fetch_row($result_category))
{
	header("Content-type: image/jpeg");
	echo "$row[0]" ;
}	
?>


Was This Post Helpful? 0
  • +
  • -

#11 chuck49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 08

Re: Display Image From MySQL Database

Posted 12 May 2008 - 05:55 AM

I also have a problem trying to get an image from mysql database.
I have followed all these examples as listed in the forum here and no luck getting an image to display.
Is there anyone there to help in this problem?

View PostBoneXXX, on 3 May, 2008 - 11:43 PM, said:

Thank you for the reply.
		//connect to the db
		$db = mysql_connect("localhost", "eventman", "farside") or die("Could not connect: " . mysql_error());

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

		//get the image name we're after
		$image = stripslashes($_REQUEST[imname]);

		//build our query
		//NEVER use select *
		$sql_query = "select ImageContent from Logo where ImageTitle=\"Utas_vert".addslashes($image).".jpg\"";

		//get the results of the query
		$query_results = mysql_query("$sql_query") or die("Invalid query: " . mysql_error());

		// set the header for the image
		header("Content-type: image/jpeg");
		echo '<img  src="'.mysql_result($query_results, 0).'">';
		// close the db link
		mysql_close($db);



And call it from a diffrent page:
echo "<img src='imagedisp.php?imname=Utas_vert'>";



And the output was just a blank page with text with the URL address(http://URL_ADDRESS/imagedisp.php)

Was This Post Helpful? 0
  • +
  • -

#12 chuck49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 08

Re: Display Image From MySQL Database

Posted 12 May 2008 - 06:06 AM

Sorry New as you can see to this forum, :)

I too am having a problem displaying an image stored in my sqldb.
I have followed all the example codes here with in and still no image displayed.

All I get is binary code on a page or a empty image with an red X.
I have been struggling with this for the last several days. :crazy:

Is there anyone there that could advise me on what Im could do to get this to work?
Was This Post Helpful? 0
  • +
  • -

#13 cbowen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-August 12

Re: Display Image From MySQL Database

Posted 25 August 2012 - 02:26 PM

View PostMartyr2, on 03 May 2008 - 11:01 AM, said:

First of all, storing images directly into a database is bad. A big no no. This causes a ton of problems from bloating your database to things like having to fetch the graphic back out.


After reading this, I knew others would read it as well. This is WRONG, WRONG, WRONG. Modern databases are very well designed for images and binary large objects like images are handled just fine. Web sites that store pictures (and video for that matter) will put them in the database because otherwise the scripts need write permission for directories and end up using the file system as a database, which it is very poor at.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1