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!
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.
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.
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.
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.
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.
This post has been edited by BoneXXX: 3 May, 2008 - 10:41 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:
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!"; }
//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);
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
<? //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
//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.
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);