By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Warning: Cannot modify header information

P: 6
Hi, I cant change the header to display images from a database. It's giving me this error :

Warning: Cannot modify header information - headers already sent by (output started at /home/content/d/f/m/dfmcons/html/secwhouse/index.php:18) in /home/content/d/f/m/dfmcons/html/secwhouse/index.php on line 71

Here is the code :
[php]
<?
mysql_connect("$host","$username", "$password");
mysql_select_db("secwhouse");
?>
<head>
<title>Security Warehouse Sample Concept</title>
</head>
<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<th scope="col"><table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/
shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="750" height="125">
<param name="movie" value="secbanner2.swf" />
<param name="quality" value="high" />
<embed src="secbanner2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="750" height="125"></embed>
</object></th>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><form id="mnusrch" name="mnusrch" method="post" action="">
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><input type="text" name="textfield" /></th>
<th scope="col"><img src="images/sec_01.gif" width="72" height="30" /></th>
<th scope="col"><img src="images/sec_03.gif" width="359" height="30" /></th>
<th scope="col"><img src="images/sec_04.gif" width="74" height="30" /></th>
<th scope="col"><img src="images/sec_05.gif" width="98" height="30" /></th>
</tr>
</table>
</form>
</th>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><img src="images/menu_01.gif" width="91" height="14" /></th>
<th scope="col"><img src="images/menu_02.gif" width="92" height="14" /></th>
<th scope="col"><img src="images/menu_03.gif" width="104" height="14" /></th>
<th scope="col"><img src="images/menu_04.gif" width="110" height="14" /></th>
<th scope="col"><img src="images/menu_05.gif" width="91" height="14" /></th>
<th scope="col"><img src="images/menu_06.gif" width="105" height="14" /></th>
<th scope="col"><img src="images/menu_07.gif" width="157" height="14" /></th>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col">&nbsp;</th>
</tr>
</table>
<?
$sql = "SELECT * FROM `prod` WHERE `prod`.`special` = 'n'";
$counter=0;
$result =mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
$count=0;
while($count<>5)
{
?>
<table border="0" cellspacing="0" cellpadding="0" id="main">
<tr>
<th scope="col"><table border="0" cellpadding="0" cellspacing="0" id="left1">
<tr>
<th scope="col"><? header("Content-type: image/jpeg");echo $rows['image']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['prod']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['price']; ?></th>
</tr>
</table></th>
<th scope="col">&nbsp;</th>
<th scope="col"><table border="0" cellspacing="0" cellpadding="0">
<tr>
<?
$rows=mysql_fetch_array($result);
?>
<th scope="col"><? echo $rows['image']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['prod']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['price']; ?></th>
</tr>
</table></th>
<th scope="col">&nbsp;</th>
<th scope="col"><table border="0" cellpadding="0" cellspacing="0" id="right1">
<tr>
<th scope="col">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
</table></th><? $count=5 ?>
</tr>
</table><? } }?>
<br />
<p>&nbsp;</p>
<p><br />
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p></th>
</tr>
</table>
</body>
</html>
[/php]I left out the connection details, cos the password is visible

That is not the only thing you left out. You must enclose all code within the appropriate code tags. Read the Posting Guidelines. - moderator
Feb 29 '08 #1
Share this Question
Share on Google+
22 Replies


hsriat
Expert 100+
P: 1,654
Post your code again after masking the password, and applying [php] tags.

Now you have disordered line 18 and 71, the actual culprits.
Feb 29 '08 #2

nathj
Expert 100+
P: 938
Hi,

The headers have to be set before any output is given. Output includes blank lines in the file. you can process information before the headers are set you just can't write anything to the screen.

Perhaps that's the problem.

Cheers
nathj
Feb 29 '08 #3

Markus
Expert 5K+
P: 6,050
I cant Even see any headers being sent =/
Feb 29 '08 #4

nathj
Expert 100+
P: 938
I cant Even see any headers being sent =/

Nor can I but I'm guessing there something in the code that was left out.

Perhaps you could post the full code sample, using tags, but simply change the sesitive data to indicate what shuold be there.

nathj
Feb 29 '08 #5

100+
P: 103
Surely line 66

Expand|Select|Wrap|Line Numbers
  1. header("Content-type: image/jpeg");
is your error...
Feb 29 '08 #6

Markus
Expert 5K+
P: 6,050
Surely line 66

Expand|Select|Wrap|Line Numbers
  1. header("Content-type: image/jpeg");
is your error...
Indeed it is. :D

Can't believe i missed that :(

Ohwell, move that line to the beggining of your script.
Feb 29 '08 #7

hsriat
Expert 100+
P: 1,654
Indeed it is. :D

Can't believe i missed that :(

Ohwell, move that line to the beggining of your script.

NO!!! Its not an image file!

Instead of <? header("Content-type: image/jpeg");echo $rows['image']; ?>, write <img src="<?php echo $rows['image']; ?>"></img>
Mar 1 '08 #8

hsriat
Expert 100+
P: 1,654
*If $rows['image'] referrers to the name of the image file.
Mar 1 '08 #9

Markus
Expert 5K+
P: 6,050
NO!!! Its not an image file!

Instead of <? header("Content-type: image/jpeg");echo $rows['image']; ?>, write <img src="<?php echo $rows['image']; ?>"></img>
Calm down.
I was tired last night _
Mar 1 '08 #10

hsriat
Expert 100+
P: 1,654
Calm down.
I was tired last night _
I'm calm :)
And the OP isn't back!
Mar 1 '08 #11

P: 6
dfm
[PHP]<?
$host="******";
$username="******";
$password="*******";
$dbname="secwhouse";
mysql_connect("$host","$username", "$password");
mysql_select_db("$dbname");
header("Content-type: image/jpeg");
?>[/PHP[HTML]]<head>
<title>Security Warehouse Sample Concept</title>
</head>
<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<th scope="col"><table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="750" height="125">
<param name="movie" value="secbanner2.swf" />
<param name="quality" value="high" />
<embed src="secbanner2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="750" height="125"></embed>
</object></th>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><form id="mnusrch" name="mnusrch" method="post" action="">
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><input type="text" name="textfield" /></th>
<th scope="col"><img src="images/sec_01.gif" width="72" height="30" /></th>
<th scope="col"><img src="images/sec_03.gif" width="359" height="30" /></th>
<th scope="col"><img src="images/sec_04.gif" width="74" height="30" /></th>
<th scope="col"><img src="images/sec_05.gif" width="98" height="30" /></th>
</tr>
</table>
</form>
</th>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><img src="images/menu_01.gif" width="91" height="14" /></th>
<th scope="col"><img src="images/menu_02.gif" width="92" height="14" /></th>
<th scope="col"><img src="images/menu_03.gif" width="104" height="14" /></th>
<th scope="col"><img src="images/menu_04.gif" width="110" height="14" /></th>
<th scope="col"><img src="images/menu_05.gif" width="91" height="14" /></th>
<th scope="col"><img src="images/menu_06.gif" width="105" height="14" /></th>
<th scope="col"><img src="images/menu_07.gif" width="157" height="14" /></th>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col">&nbsp;</th>
</tr>
</table>[/HTML] [PHP] <?
$sql = "SELECT * FROM `prod` WHERE `prod`.`special` = 'n'";
$counter=0;
$result =mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
$count=0;
while($count<>5)
{
?>[/PHP] [HTML] <table border="0" cellspacing="0" cellpadding="0" id="main">
<tr>
<th scope="col"><table border="0" cellpadding="0" cellspacing="0" id="left1">
<tr>
<th scope="col">[/HTML[PHP]]<? echo $rows['image']; ?>[/PHP[HTML]]</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">[/HTML[PHP]]<? echo $rows['prod']; ?>[/PHP][HTML]</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">[/HTML][PHP]<? echo $rows['price']; ?>[/PHP][HTML]</th>
</tr>
</table></th>
<th scope="col">&nbsp;</th>
<th scope="col"><table border="0" cellspacing="0" cellpadding="0">
<tr>[/HTML]
[PHP]<?
$rows=mysql_fetch_array($result);
?>[/PHP][HTML] <th scope="col">[/HTML][PHP]<? echo $rows['image']; ?>[/PHP][HTML]</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">[/HTML][PHP]<? echo $rows['prod']; ?>[/PHP][HTML]</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">[/HTML][PHP]<? echo $rows['price']; ?>[/PHP][HTML]</th>
</tr>
</table></th>
<th scope="col">&nbsp;</th>
<th scope="col"><table border="0" cellpadding="0" cellspacing="0" id="right1">
<tr>
<th scope="col">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
</table></th>[/HTML][PHP]<? $count=5 ?>[/PHP]
[HTML]</tr>
</table>[/HTML][PHP]<? } }?>[/PHP]
[HTML] <br />
<p>&nbsp;</p>
<p><br />
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p></th>
</tr>
</table>
</body>
</html>[/HTML]

THANKS FOR ALL THE REPLIES, I MOVED THE HEADER CODE TO THE BEGINNING OF THE SCRIPT, THE ERROR IS GONE BUT THE IMAGE DOESN'T DISPLAY , IT JUST SHOWS FUNNY CHARACTERS!!!!!
Mar 2 '08 #12

P: 6
dfm
[php]<?
$host="******";
$username="******";
$password="*******";
$dbname="secwhouse";
mysql_connect("$host","$username", "$password");
mysql_select_db("$dbname");
header("Content-type: image/jpeg");
?><head>
<title>Security Warehouse Sample Concept</title>
</head>
<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<th scope="col"><table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/
cabs/flash/swflash.cab#version=7,0,19,0" width="750" height="125">
<param name="movie" value="secbanner2.swf" />
<param name="quality" value="high" />
<embed src="secbanner2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="750" height="125"></embed>
</object></th>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><form id="mnusrch" name="mnusrch" method="post" action="">
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><input type="text" name="textfield" /></th>
<th scope="col"><img src="images/sec_01.gif" width="72" height="30" /></th>
<th scope="col"><img src="images/sec_03.gif" width="359" height="30" /></th>
<th scope="col"><img src="images/sec_04.gif" width="74" height="30" /></th>
<th scope="col"><img src="images/sec_05.gif" width="98" height="30" /></th>
</tr>
</table>
</form>
</th>
</tr>
</table>
<table width="750" border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col"><img src="images/menu_01.gif" width="91" height="14" /></th>
<th scope="col"><img src="images/menu_02.gif" width="92" height="14" /></th>
<th scope="col"><img src="images/menu_03.gif" width="104" height="14" /></th>
<th scope="col"><img src="images/menu_04.gif" width="110" height="14" /></th>
<th scope="col"><img src="images/menu_05.gif" width="91" height="14" /></th>
<th scope="col"><img src="images/menu_06.gif" width="105" height="14" /></th>
<th scope="col"><img src="images/menu_07.gif" width="157" height="14" /></th>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<th scope="col">&nbsp;</th>
</tr>
</table>
<?
$sql = "SELECT * FROM `prod` WHERE `prod`.`special` = 'n'";
$counter=0;
$result =mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
$count=0;
while($count<>5)
{
?>
<table border="0" cellspacing="0" cellpadding="0" id="main">
<tr>
<th scope="col"><table border="0" cellpadding="0" cellspacing="0" id="left1">
<tr>
<th scope="col"><? echo $rows['image']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['prod']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['price']; ?></th>
</tr>
</table></th>
<th scope="col">&nbsp;</th>
<th scope="col"><table border="0" cellspacing="0" cellpadding="0">
<tr>
<?
$rows=mysql_fetch_array($result);
?>
<th scope="col"><? echo $rows['image']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['prod']; ?></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row"><? echo $rows['price']; ?></th>
</tr>
</table></th>
<th scope="col">&nbsp;</th>
<th scope="col"><table border="0" cellpadding="0" cellspacing="0" id="right1">
<tr>
<th scope="col">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
</tr>
</table></th><? $count=5 ?>
</tr>
</table><? } }?>
<br />
<p>&nbsp;</p>
<p><br />
</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p></th>
</tr>
</table>
</body>
</html>[/php]

I moved the header code to the beginning of the script , the error is gone but it doesn't display the image, it just displays funny characters! please help,
Mar 2 '08 #13

Markus
Expert 5K+
P: 6,050
Look at post #8

And dont type in caps.
Mar 2 '08 #14

ronverdonk
Expert 2.5K+
P: 4,258
Don't overdo you tags now. Just one pair at start and end of your code suffices.

moderator
Mar 2 '08 #15

P: 6
dfm
I tried that, its displaying a picture box with a red x in it and the funny characters next to it ?
Mar 3 '08 #16

nathj
Expert 100+
P: 938
I tried that, its displaying a picture box with a red x in it and the funny characters next to it ?
If the data has the path to the image in it and this is where the image then is it should show just fine. Perhaps the image isn't wwhere the database says it is?

Did you place any alt text in the image text? This should display if the image can't be found.

Double check the file path of the image and check this path in relation to the path of the code being executed.

If you are just using jpg and <img> tags then the header is not strictly necessary as far as I am aware.

Cheers
nathj
Mar 3 '08 #17

hsriat
Expert 100+
P: 1,654
I tried that, its displaying a picture box with a red x in it and the funny characters next to it ?

What were you actually trying to get by using that header?..
Also post the HTML generated by that part of the code here.
Mar 3 '08 #18

P: 6
dfm
[HTML]O;k2:S
8 |}y 5
{w,0p?>Ϋq$XQ|oex
kk`y(_ \{N]_p䂦&
/'Z
=f C}oӈiYv
a)a)a)a)a)a)a)a)a)a
)a)a)a)a+ؔ9g,I<s
S>5LonN=RS
=Q=QN9-%.8K2
R㌔%.8K2R
%.8K2R㌔%.8K2W
]du^?t::*ʊ*y5G ,
R,RKn.Ժ%.K2R
댔%.
K2R댔%.K2R댔%.
K2Q{C%F=MyD
>}8ưuRÈRÈRÈRÈW~2h
&2h&2h&2h&2h
&2h&2h&2h&v
*7\R,;rhrE,K
.2RˌҾrd}J_qE)}
Md)}J_qd}J_q
d)}J_qd9;PyTW
 qKpSV˖)iJZq9Є
v3 h=v{ 3ݡ!gB=v
3ݡ!gB=v3 sqy
?᧍N1 "> [/HTML]

I have a mysql database to which I uploaded an image not a the path to an image but the actual image. When using a test.php file to display it displays the image fine. It gave me the same error when I used a function.php with the connection string, so I deleted it and put the connection string in the actual page and it worked. But now on the actual page I need to use it , it doesnt want to display?
Mar 3 '08 #19

P: 6
dfm
meant put the connection string in the test page, sorry
Mar 3 '08 #20

nathj
Expert 100+
P: 938
Why not just have the path to the image stored in the database and then use that information in a normal <img> tag?

This would be much simpler and not require headers.

Cheers
nathj
Mar 3 '08 #21

hsriat
Expert 100+
P: 1,654
For that you would need to make another PHP file. Say get_image.php with the following code:[php]<php
if (!isset($_GET['id'])) die('Invalid Parameter');
/*
DO - CONNECT TO DATABASE
FETCH ARRAY FROM THE DB, WHERE unique_id_of_the_table = $_GET['id']
*/
header("Content-type: image/jpeg");
echo $rows['image'];
?>[/php]
And edit line no. 71 of your above file like this:
[html]<th scope="col"><img src="get_image.php?id=<? echo $rows['unique_id_of_the_table']; ?>"></img></th>[/html]

* unique_id_of_the_table : whatever unique id you are using.
Mar 3 '08 #22

nathj
Expert 100+
P: 938
Maybe I'm missing something, and I'm perfectly happy to admit that, but is the point of putting the image itself into the database? Surely it is easier to just put the image as a file on the server somewhere and then put the file path in the database. This can then be used in a nomal <img> tag without any need for faffing around with headers.

It just seems to make more sense to me that way. Obviously you can't set the src of the img to be the actual image which is why it didn't work earlier.

If it were me I would have fields in the table called image_src, image_title and image_alt. Then assuming the SQL had been run and the following code is inside a foreach loop:
[php]
echo "<img src='" . $laRow['image_src'] . "' title='" . $laRow['image_title'] . "' alt='" . $laRow['image_alt'] ."' />' ;
[/php]

I am aware that this is a small code snippet, I would have this in a foreach loop after the SQL returned an associative array. I use this type of structure a lot and it works everytime.

Cheers
nathj
Mar 3 '08 #23

Post your reply

Sign in to post your reply or Sign up for a free account.