Jump to content

Displaying an image from MySQLdb


nac1987

Recommended Posts

I must be doing something incredibly daft, because I'm incredibly new at this.

 

I have an image stored in a DB under a table called 'images' and I want to display it on my website but instead of that image I get the error: Warning: Cannot modify header information - headers already sent by (output started at /home/...

 

This is how I'm trying to achieve it.

Any ideas where I'm doing wrong?

 

Thanks.

 

 

<?php
$user="###";
$password="###";
$database="###";
$con = mysql_connect(localhost,$user,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($database, $con);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MySite</title>
</head>   
<body>
<div id="container">
<?php include("../navbar.php"); ?>
<div id="left-content"></div>
<div id="right-content">
<?php
   $item = $_GET['item'];
   $query = "SELECT * FROM main WHERE Ref='$item'";
   $result = mysql_query($query)
   or   die("Oops" .mysql_error());
   $row = mysql_fetch_array($result,MYSQL_BOTH)
   or   die("Oops" .mysql_error());
   extract($row);
   $query2 = "SELECT image FROM main WHERE Ref='$item'";

// the result of the query
$result2 = mysql_query($query2) or die("Invalid query: " . mysql_error());
header("Content-type: image/jpg");
echo mysql_result($result2, 0,'image');
echo "<p><strong>Name: </strong>".$FirstName." - ".$SecondName."</p>";
mysql_close($con);
?> 
</div>
<br />
<?php include("../footer.php"); ?>
</div>
</div>
</body>
</html>

 

MOD EDIT:

 . . . 

BBCode tags added.

Link to comment
Share on other sites

I am sorry.

I'll remember the code tags for future postings.

I've just read the other thread. I'm still a bit at a loss. A lot of the code you see there is just typed in from a book, I don't fully understand what a header is.

Should this bit of code that states that the variable is a jpg image

 

This bit:

 

header("Content-type: image/jpg");

 

Is that different to a <head> </head> section of a web page?

 

Ignorance isn't bliss.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.