Welcome to Dream.In.Code
Click Here
Getting PHP Help is Easy!

Join 118,922 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,985 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Display Image From MySQL Database

 
Reply to this topicStart new topic

Display Image From MySQL Database

BoneXXX
post 3 May, 2008 - 03:45 AM
Post #1


New D.I.C Head

*
Joined: 26 Sep, 2007
Posts: 23


My Contributions


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

CODE

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?
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 3 May, 2008 - 11:01 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,669



Thanked 125 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


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...

CODE

<?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...

CODE

<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.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

BoneXXX
post 3 May, 2008 - 10:29 PM
Post #3


New D.I.C Head

*
Joined: 26 Sep, 2007
Posts: 23


My Contributions


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.

CODE

<?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
------------------------------------------------------------------------------------------------------------------------------------

QUOTE(Martyr2 @ 3 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...

CODE

<?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...

CODE

<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.

smile.gif


This post has been edited by BoneXXX: 3 May, 2008 - 10:41 PM
User is offlineProfile CardPM

Go to the top of the page

BoneXXX
post 3 May, 2008 - 11:02 PM
Post #4


New D.I.C Head

*
Joined: 26 Sep, 2007
Posts: 23


My Contributions


I am able to display the image with this code
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: 3 May, 2008 - 11:28 PM
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 3 May, 2008 - 11:12 PM
Post #5


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,918



Thanked 83 times

Dream Kudos: 8100

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI, Boo.Net

My Contributions


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:


php

//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 smile.gif
User is offlineProfile CardPM

Go to the top of the page

BoneXXX
post 3 May, 2008 - 11:43 PM
Post #6


New D.I.C Head

*
Joined: 26 Sep, 2007
Posts: 23


My Contributions


Thank you for the reply.
CODE

        //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:
CODE

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)
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 3 May, 2008 - 11:54 PM
Post #7


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,918



Thanked 83 times

Dream Kudos: 8100

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI, Boo.Net

My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

BoneXXX
post 4 May, 2008 - 03:04 AM
Post #8


New D.I.C Head

*
Joined: 26 Sep, 2007
Posts: 23


My Contributions


Ok. This is exactly what I did:

Imagedisp.php
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

QUOTE(PsychoCoder @ 3 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


This post has been edited by BoneXXX: 4 May, 2008 - 04:43 AM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 4 May, 2008 - 09:57 AM
Post #9


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,669



Thanked 125 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


CODE

//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.

smile.gif
User is offlineProfile CardPM

Go to the top of the page

BoneXXX
post 11 May, 2008 - 09:51 PM
Post #10


New D.I.C Head

*
Joined: 26 Sep, 2007
Posts: 23


My Contributions


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

<?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]" ;
}
?>

User is offlineProfile CardPM

Go to the top of the page

chuck49
post 12 May, 2008 - 05:55 AM
Post #11


New D.I.C Head

*
Joined: 12 May, 2008
Posts: 2

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?

QUOTE(BoneXXX @ 3 May, 2008 - 11:43 PM) *

Thank you for the reply.
CODE

        //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:
CODE

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)

User is offlineProfile CardPM

Go to the top of the page

chuck49
post 12 May, 2008 - 06:06 AM
Post #12


New D.I.C Head

*
Joined: 12 May, 2008
Posts: 2

Sorry New as you can see to this forum, smile.gif

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.gif

Is there anyone there that could advise me on what Im could do to get this to work?
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 04:56AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month