PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 20, 2014 3:49 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Jun 04, 2012 4:39 am 
Offline
Forum Newbie

Joined: Sun May 13, 2012 6:41 am
Posts: 4
I'm trying to display an image form the mysql database.

I have this code. But it returns, "Warning: Cannot modify header information - headers already sent by (output started at E:\Softwares\WAMP\wamp\www\site\index.php:5)"

When i commented the "header('Content-type: ' . $row_getImage['mimetype']);" line, it will not display an error though displays something like hashcode instead of the image. Please look at the attachment.

What can I do for this?

Syntax: [ Download ] [ Hide ]
<?php
        $connect = mysql_connect("localhost", "root", "123") or die ("Error, check your server connection.");
        $database_testConn = mysql_select_db("test1") or die("cannot select DB");

        $query_getImage = "SELECT mimetype, image FROM images";

        $getImage = mysql_query($query_getImage, $connect) or die(mysql_error());

        while($row_getImage = mysql_fetch_array($getImage)){

                header('Content-type: ' . $row_getImage['mimetype']);
                echo $row_getImage['image'];
                mysql_free_result($getImage);
        }
        mysql_close();
?>


Attachments:
File comment: hash
hash.jpg
hash.jpg [ 631.89 KiB | Viewed 262 times ]
Top
 Profile  
 
PostPosted: Mon Jun 04, 2012 7:21 am 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2775
Location: .za
http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php
this url may be of some assistance;

The warning you are receiving is because headers have to be sent before any other output on the page. What you are seeing is the image in an incorrect format (not jpeg).

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Mon Jun 04, 2012 2:05 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6434
Location: 53.01N x 112.48W
If possible, do not store your images in a database. I know it sounds appealing because you can simply have all the data you need from one query, but it's a bad idea. The database was not intended to hold image data - that's what the filesystem is for. Just store a path to the file in the record, and let the web server deal with sending it to the browser.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group