boneXXX Posted May 3, 2008 Share Posted May 3, 2008 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. �����JFIF��H�H�����C����C�������������������� ���h�� � !"#1AX��$2Wx���%389BQVw��&6Rav����(47Y���'5CDTUq��ESc����)bu������������������ ���j� �! "1#2A$Q3Bau�%68RTUqw����457CSVWbtv��������&DX����cr����ds��'ef���������?����D�&0��VgʯP�p�0�<�����t��Ym What could cause the problem.? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/ Share on other sites More sharing options...
mrdamien Posted May 3, 2008 Share Posted May 3, 2008 You need to send the content-type header header("Content-type: image/png"); png/jpeg/gif etc The gibberish is the contents of the image file, but you need to tell the browser to render it like a picture. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532296 Share on other sites More sharing options...
boneXXX Posted May 3, 2008 Author Share Posted May 3, 2008 Thank you for the reply. I already did that but still i am getting the gibberish content of the image. This is some part of the code <form method="post" action="admin_home.php"> <tr> <td width="430" align="center"><strong><u>Logo</u></strong><td/> <td width="550" align="center"><strong><u>Action</u></strong><td/> <tr/> <? $query_category="SELECT ImageContent,ImageTitle, Event_Name FROM Logo"; $result_category = mysql_query($query_category); while($row=mysql_fetch_row($result_category)){ ?> <tr> <td width="430" align="center"> <? header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo "$row[0] - $row[1] - $row[2]" ; ?><td/> <td width="550" align="center"> <? echo "<a href=\"delete_logo.php?Logo=$row[0]&RaceDist=$row[1]&Event=$row[2]\"><button>Remove Logo</button><a/> ";?><td/> <tr/> <? } ?> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532299 Share on other sites More sharing options...
boneXXX Posted May 3, 2008 Author Share Posted May 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532306 Share on other sites More sharing options...
boneXXX Posted May 3, 2008 Author Share Posted May 3, 2008 Any advise? Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532341 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2008 Share Posted May 3, 2008 You need to use proper html tags on a web page and you cannot output image data directly on a web page - http://www.w3schools.com/html/html_images.asp Each image on a web page requires an <img src="url_that_results_in_an_image" alt=""> tag. The url_that_results_in_an_image in the above must be to a .php file that outputs the header and the image data. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532404 Share on other sites More sharing options...
boneXXX Posted May 4, 2008 Author Share Posted May 4, 2008 Thank you for the reply. The problem is not that I am sure about it,because i also tried with echo "<img src='$row[0]' width='75' height='75'> . Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532577 Share on other sites More sharing options...
mrdamien Posted May 4, 2008 Share Posted May 4, 2008 No..... try reading PFMaBismAd's post again. image.php <?php $id = (int)$_GET['id']; include("dbconnect.php"); $query = "SELECT ImageContent FROM Logo WHERE id = " . $id; $results = mysql_query($query); $data = mysql_fetch_array($results); header("Content-type: image/jpeg"); echo $data[0]; ?> test.php <img src="image.php?id=123" /> Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532581 Share on other sites More sharing options...
boneXXX Posted May 4, 2008 Author Share Posted May 4, 2008 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]"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532589 Share on other sites More sharing options...
dualshock03 Posted May 4, 2008 Share Posted May 4, 2008 Hey i want to clarify about this.. i try all your sample codes but dsnt work.. still some binary codes appear.. any other ways?? im using dreamweaver CS3 Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-532635 Share on other sites More sharing options...
boneXXX Posted May 5, 2008 Author Share Posted May 5, 2008 Is there any other way that you would suggest for storing image and displaying it? Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-533265 Share on other sites More sharing options...
dualshock03 Posted May 5, 2008 Share Posted May 5, 2008 Ohh Man... need to wait long... im tired searching within 3 days, still thesame samples and suggestions.. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-533362 Share on other sites More sharing options...
RichardRotterdam Posted May 5, 2008 Share Posted May 5, 2008 what i usually do i just upload the image in a folder and store the image url as a string. but i guess blob type has some advantages. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-533635 Share on other sites More sharing options...
dualshock03 Posted May 7, 2008 Share Posted May 7, 2008 I just found this article... and im going for it, no other choice.. it stores the image to the database and retrieves the image as string.. http://www.phpbuilder.com/columns/florian19991014.php3?page=1 http://www.phpbuilder.com/columns/florian19991014.php3?page=2 http://www.phpbuilder.com/columns/florian19991014.php3?page=3 hope this solves the entire problem.. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-535219 Share on other sites More sharing options...
boneXXX Posted May 12, 2008 Author Share Posted May 12, 2008 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]" ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-538756 Share on other sites More sharing options...
PFMaBiSmAd Posted May 12, 2008 Share Posted May 12, 2008 Each image requires a HTML <img src="url_that_results_in_an_image" alt=""> tag on the web page and the url_that_results_in_an_image must output that corresponding single image (header and image data.) Some form of an id number or the image name that is related to each image in your database is usually used on the end of the url - <img src="your_php_code.php?id=1" alt=""> <img src="your_php_code.php?id=2" alt=""> <img src="your_php_code.php?id=3" alt=""> Then in your php code that is fetching the image from the database, you use $_GET['id'] to retrieve and output the correct header and image data for that <img tag. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-538908 Share on other sites More sharing options...
kev wood Posted May 12, 2008 Share Posted May 12, 2008 the best way to go about it that i have found is not actually storing the images in the database rather just save the path to the image. the code i have used looks like this $broad1name2="image/thumbs/thumb_".$image_name this section of the code stores the path to the file into a variable. the next section stores the variable in the mysql database. $sql="INSERT INTO images_broad (broad1) VALUES ('$broad1name2')"; $query = mysql_query($sql); to display the images from the database the code i used looks like this <?php $query = "SELECT broad1 FROM images_broad"; $result=mysql_query($query); while($row=mysql_fetch_array($result, MYSQL_ASSOC)){ echo '<img src="'.$row['broad1'].'"/>'; } to make things a little bit clearer i will show you the code i used to set the table up so the code above makes a little bit more sense. $sql="CREATE TABLE IF NOT EXISTS `images_broad` ( `id` INT NOT NULL AUTO_INCREMENT, `broad1` varchar (250) not null, `broad2` varchar(250) not null, primary key (id) )"; mysql_query($sql) or die (mysql_error()); hope this sorts out what you was looking for. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-538957 Share on other sites More sharing options...
boneXXX Posted May 17, 2008 Author Share Posted May 17, 2008 Thank you for the replies I did it by using $_get[id] method. It is working fine. Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-543390 Share on other sites More sharing options...
dualshock03 Posted May 22, 2008 Share Posted May 22, 2008 can you show me ur code on it?? was it directly from the database query? Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-547113 Share on other sites More sharing options...
boneXXX Posted May 24, 2008 Author Share Posted May 24, 2008 I followed this codes to make it work with my codes. http://www.phpriot.com/articles/images-in-mysql http://www.php-mysql-tutorial.com/php-mysql-upload.php Quote Link to comment https://forums.phpfreaks.com/topic/103972-getting-an-image-from-mysql-database/#findComment-548692 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.