BLOB image appearing as characters

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

BLOB image appearing as characters

Post by khushbush »

I've been trying to get images to show up on my search page, but it keeps showing up as random characters, e.g. ‰usªêÂÚØæÊÙ€Û£

My images are stored as BLOBs in the database...I've already tried storing and displaying images as file pathnames, but to no avail.
I've tried most methods by Googling my problem, but it still doesn't seem to work. I've saved the image display method in one file, and then called that image in another php file and displayed it using the <img src> tag...and yet it still shows up funny characters.

I am going to post the code up for both php files.

This is code from showimage.php:

Code: Select all

 
<?php
  header("Content-Type: image/jpeg");
 
  mysql_connect(" ", " ", " ") or die(mysql_error());
 
  mysql_select_db("property") or die(mysql_error()); 
 
 
 
 // Put the value in a separate variable
  $propID = ($_GET['id']); 
  $query = "select propertyImage1, propertyImage2, propertyImage3 from property where propertyID = $propID";
  $result = mysql_query($query) or die(mysql_error());
 
 
if($row = mysql_fetch_array($result)){
  $imageData1 = $row['propertyImage1'];
}
header('Content-Type: image/jpeg');
echo $imageData1;
?>
 
And here is the code from searchprocess.php where the image is to be displayed:

Code: Select all

 
<HTML>
<BODY>
<table ALIGN = "LEFT">
<font face="Arial" color="#0B3A62">
 
<?//display results?>
 
Property area: <? echo $row[0]; ?> , 
Starting bid: £<? echo $row[1]; ?> , 
<? echo $row[2]; ?> ,
<? echo $row[3]; ?> bedroomed,
<? echo $row[4];?>
<? echo $row[5];?>
   <br>
   <br>
   <tr><tr><td><?echo "<img src =\"showimage.php?id=$row[propertyID]\">";?></td></tr></tr>
   </br></br>
 
Any thoughts/ideas?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: BLOB image appearing as characters

Post by onion2k »

According to the code you posted for showimage.php the opening PHP tag (<?php) is on line 2. That means you're outputting a carriage return first. That will be enough to break the image.

You're also outputting the content type header twice but that shouldn't be a problem.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: BLOB image appearing as characters

Post by khushbush »

No, that's just the way I posted the code...apologies for that. :oops:
There are no extra lines or spaces above the php tag.

The double header doesn't make a difference, as I am able to display images in another file. Can't seem to do the same with this one... :?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB image appearing as characters

Post by pickle »

My guess would be that you've got a header conflict. Use FF & download an extension that captures HTTP headers. That might give you an idea.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: BLOB image appearing as characters

Post by khushbush »

Erm...FF? :oops: Could you please elaborate?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB image appearing as characters

Post by pickle »

Sorry - Firefox.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: BLOB image appearing as characters

Post by khushbush »

I already use Firefox...in fact, I practically swear by it! Ok...I'm going off topic...erm...should I Google a HTTP header capture thingy? I guess what I mean to ask is...where would be the best place to download an extension that captures HTTP headers? PHP.net?
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: BLOB image appearing as characters

Post by khushbush »

Ok, silly me...did you mean something like this?

https://addons.mozilla.org/en-US/firefox/addon/3829
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB image appearing as characters

Post by pickle »

Ya -something like that.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: BLOB image appearing as characters

Post by khushbush »

I'm unsure how to use it. What am I supposed to be looking for?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB image appearing as characters

Post by pickle »

Look through the header logs for the Content-Type header you sent. I'm guessing there is a Content-Type header being sent before the ones you intend, that change the type from image/jpeg, to text or something else.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: BLOB image appearing as characters

Post by khushbush »

Well...it says that the content-type header is set to text/html on the page where I want images to show up...but showimage.php has a content-type header set to image/jpeg...I also think that the problem may lie in the fact that showimage.php is not accessing the image corresponding to the id specified. Could that be a possible cause for the image breaking up?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB image appearing as characters

Post by pickle »

If showimage.php is showing a file that isn't a JPEG image, that's a possibility. It might also be a possibility that the image data was truncated in the database, so you're not getting a complete image.

Remove the first header() so you just set 1. Then echo your query to see if its what you expect.

Also, you don't need parentheses around $_GET['id'] when setting $propID. You also don't need to be retrieving, the propertyImage2 or propertyImage3 fields in your query.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
khushbush
Forum Commoner
Posts: 99
Joined: Tue Mar 11, 2008 11:50 am

Re: BLOB image appearing as characters

Post by khushbush »

Ok, so I've decided to include showimage.php in searchprocess.php...and the image shows up. Unfortunately, I can't get the desired data to show up from the database...so what can I do to get both the picture and data to show up together?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: BLOB image appearing as characters

Post by pickle »

That's a different question than you originally asked correct? Best to open a new thread for that as it will likely have different problems.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply