Hello I'm trying to display images from my database using blob but the browser diplays them in a unusual way.

This is what I get!!!
<82<.231 1!!11111111111111111111111111111111111111111111111111������������� ������� ���}�!1AQa"q2���#B��R��$3br�

What is it that i'm doing wrong? I tried adding a header(contect-type:image.jpeg) but is not working for me :(

Here is the code:

<?php
//database info in misc.inc
include("misc.inc");
//Get item ID
$id = $_GET["itemID"];
$db = 0;
//Connect to database
$db = mysql_connect($db_host . ":" . $db_port, $db_user, $db_pwd);
if ($db == 0) {die("Cannot connect, Error <b>" . mysql_errno() . "</b>: " . mysql_error());};

mysql_query("USE " . $db_db);
if (mysql_errno() != 0) {die("Cannot USE database, Error <b>" . mysql_errno() . "</b>: " . mysql_error());};
//fetch data from database
$query1 = "SELECT * FROM images, items WHERE (images.itemID='$id' && items.itemID='$id')";
$result = mysql_query($query1);
while ($row = mysql_fetch_array ($result))
{
echo"<table>";
echo"<tr>";
echo"<td colspan='2'>
  <h2 style='font-size: large;'> ".$row['itemName']."</h2></td>\n";
 echo "</tr>";
 echo "<tr>";
echo"<td><img border='0' align='right' src='../images/{$row['pix']} alt='item Image' style='margin-left: 5px;'></td>\n";
echo"<tr>";
echo"<td colspan='2'>
   <div>     
         <b>Price: </b>&pound;" .number_format ($row['price'],2). "<br />
         <b>Posted On: </b>" .$row['submitDate']. "<br />\n
         </div><br>
<b>Description: </b><p>" .$row['item_Description']. "<p><br />
 <div ;='' style=clear: both;>
  <h4 style='font-size: medium;'>Contact details:</h4>
<b>Name: </b>" .$row['contactName']. "<br />
<b>E-mail: </b>".$row['email']." <br /><br />  
</div></td>\n";
echo"</tr>\n";
}
echo "</table><br/>";
?>

Thanks a lot

How did you saved the image? Did you use upload form for image upload and store the only file name in the database ? Check manual in your mysql tables, how does the image store, and what type of the field.

Yes I use a multipart/form-data, the form is redirected to the upload.php and the only place I save the images is in the database. Because I'm trying to display images and also text from two different tables, therefore i do that by using their IDs.

upload.php

if(isset($_POST['submit']) && $_FILES['userfile']['size'] > 0)
{
$itemName = $_POST ['itemName'];
$CID = $_POST ['CID'];
$item_Description = $_POST ['item_Description'];
$price = $_POST ['price'];
$contactName = $_POST ['contactName'];
$phone = $_POST ['phone'];
$email = $_POST ['email'];
$website = $_POST ['website'];

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$pix = fread($fp, filesize($tmpName));
$pix = addslashes($pix);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
$sqlItem = "INSERT INTO items (itemName, categoryID, item_Description, price, contactName, phone, email, website, submitDate)".
		"VALUES ('$itemName', '$CID', '$item_Description', '$price', '$contactName', '$phone', '$email', '$website', CURDATE())";


if (@mysql_query ($sqlItem)) {
	  echo('<p>Thank you for your posting. If you wish your item or other items go to Classifieds</p>');
	  } 
	else {
	  echo('<p>Error adding submitted ad: ' .mysql_errno().'-'.
            mysql_error() . '</p>');
	  }
	}
    
	  echo('<p><a href="postad.php"> Post another ad!</a></p>');
	  echo ('<p><a href="Classifieds.php"> Classifieds</a></p>');
	  echo ('<p><a href="index.php"> Home</a></p>'
		);
$lastID = mysql_insert_id();

$sqlImg = "INSERT INTO images (itemID, categoryID, name, size, type, pix) "." VALUES ('$lastID', '$CID', '$fileName', '$fileSize', '$fileType', '$pix')";

mysql_query($sqlImg) or die('<p>Error, query failed ' .mysql_errno().'-'.mysql_error() .'</p>');

echo "<br>File $fileName uploaded<br>";

I have inserted an image manually but it does the same thing!
Also I added a header

header("Content-type: ".$row['type']."");

and the browser just display this:
"item_Details.php?itemID=2", which come from the classified.php (this page display items and when the user click in one of the items the page redirect you to another page item_details.php)

Here is the code that I use for classified.php

//Table 2 furniture
$query2 = "SELECT * FROM {$table} WHERE categoryID='2'";
$result = mysql_query($query2);
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);
if($numofrows == 0){
echo "<p class=spect>Furniture:</p><p>No entries yet</p>\n";
}
else{
echo"<table id=mytable cellspacing=0 summary=Items from almacen database>
<caption>Funiture: </caption>";
echo "<TR bgcolor=\"lightblue\"><TH>Item Name</TH><TH>Price</TH><TH>Posted On</TH></TR>\n"; 
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
$id=$row['itemID'];
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#F5FAFA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"#FFFFFF\">\n";
} 
echo "<TD><a href='item_Details.php?itemID=<?php=$id;?>' target = '_blank'>{$row['itemName']}</a></TD>
<TD>&pound;".number_format ($row['price'],2)."</TD><TD>".$row['submitDate']."</TD>\n";
echo "</TR>\n";
} }	
echo "</table><br/>";

Thank you for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.