View Full Version : How to Output Image from Mysql?
07-16-2007, 05:09 AM
I can't seems to get the images from mysql table. All I get are strange numbers. When i add a header like this, it gives a warning like this,
Warning: Cannot modify header information - headers already sent by.....
this is only a simple code to get the image out.
07-16-2007, 11:03 AM
we need to see a little more than that...
However if it says 'headers already sent' and there are no other errors above that then likely your problem is 'whitespace'. Check to make sure there is nothing in your page above the <?php part No output can be sent to the browser before the 'header', including blank lines
07-16-2007, 02:22 PM
but when i placed the header at the top of the page, i cannot get the html contents. i managed to view the image and it's only one image. it seems like i can't get any of the contents that i have.
07-16-2007, 02:43 PM
The only way to place an image on a web page and get a browser to render it is to use an <img src="a_url_that_outputs_an_image" alt=""> tag - http://w3schools.com/html/html_images.asp This is the only thing browsers understand. You need one <img...> tag for each image. The a_url_that_outputs_an_image needs to be to a separate .php file with your header/image echo output code in it.
This is actually a HTML coding issue. If you search the forum and/or read through the PHP manual user contributed notes under the image functions you can find out more information.
07-17-2007, 01:50 AM
Hm...I'm talking about extracting the image from mysql. If I just select from mysql db, it's some weird numbers. But when I add the header at the top of the page, I can't view any html contents.
07-17-2007, 05:27 AM
This may be a nitpick but....
Why are you storing your pictures in the DB? It may be better for you if you were to instead store the path to the image opposed the image itself. That was you can retrieve the image path and dynamically write it in your src attribute.
What data type are you using for the pic field? Long Blob?
Also, you may find this post handy. http://forums.mysql.com/read.php?20,17671,27914
07-17-2007, 05:41 AM
suddenly i figured storing in mysql may not be the best way. do u know of any sites where i can read up on storing in the server and path to mysql?
07-17-2007, 06:08 AM
07-17-2007, 06:09 AM
hopefully this answers your question. i made this script for you.
07-17-2007, 06:16 AM
also wrong studd
07-17-2007, 06:21 AM
OK THIS IS THE FINAL VERSION:
I checked it and it works: here you go: USE THIS SCRIPT NOT THE ONE ABOVE:
//Make variable for image url to be inputed
$img_url = $_POST['imageurl'];
$img_name = $_POST['imagename'];
//Then connect to database.
$con = mysql_connect("localhost", "user", "pass");
if (!mysql_connect("localhost", "user", "pass"))
echo "Could not connect";
//Then you choose the table to save info on
$sql = "INSERT INTO Table_Name (Img_name, Img_url) VALUES ('$img_name', '$img_url')";
die('Could not add image Data. Report this error to the Administrator:' . mysql_error());
Echo "Added Image Data:<br>";
Echo "<table border=1><tr><td>Image URL: </td>";
Echo "<td>" . $img_url . "</td></tr>";
Echo "<tr><td>Image name: </td>";
Echo "<td>" . $img_name . "</td></tr>";
<head><Title>Add image to database</title></head>
<h2>Add image data:</h2><br>
<form action="addimg.php" method="POST">
<tr><td>Image URL: </td><td><input type="text" name="imageurl"></td></tr>
<tr><td>Image Name: </td><td><input type="text" name="imagename"></td></tr>
<input type="Submit" Value="Add Image Data">
07-17-2007, 07:34 AM
but i'm confused as to how i should use the image header?
07-17-2007, 11:31 AM
you only need to use the image header if your using <img scr="image.php" alt...... /> and the headers go into image.php other wise use normal/no headers with <img scr="image.jpg" />
07-17-2007, 02:37 PM
i want to view the image from mysql db. once i query n select all the image from mysql, i need to add a header so that the image can be displayed correctly. but how do place/use header("Content-type: image/jpeg"); correctly? if i placed it at the top of the page, i can only get one image from mysql and the rest i don't know why i can't get it out. and also the html contents cannot be displayed. if i remove the header, the mysql image becomes broken and i get the html contents.
how do i actually use this header correctly?
07-17-2007, 02:50 PM
You cannot do what you're trying to do and just keep asking the same question wont change the answer.
Headers tell the browser what content to expect, sending a header of "content-type: image/jpeg" tells the browser to expect a single jpg image. You cant just mix it in with html, the browser is no longer expecting html, its expecting image data.
The best solution to your problem is to store the image path in the database instead of the actual image
07-17-2007, 04:23 PM
which i made the script for.... storing image url and image name. trust me dude that is the best way to do it.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.