Trying to display a blob ...

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

Post Reply
stirnpanzer
Forum Newbie
Posts: 7
Joined: Tue Jun 02, 2009 6:38 am

Trying to display a blob ...

Post by stirnpanzer »

Am trying to display some php fields in a html table....

all ok except the blob file 'person_photo' which displays as garbage...

is it syntax or me ??

Code: Select all

 
[color=#BF0000]<?php[/color]
 
$query = "select * FROM people WHERE person_country = 81" ; 
 
$data = mysql_query($query) ; 
Print $query;
 
Print "<table width='100%' height='1000' border cellpadding=1>"; 
while($info = mysql_fetch_array( $data )) 
{ 
Print "<tr>";
Print "<td height='59'>&nbsp;</td>";
Print "<td >Lastname : " .$info['person_lastname'] . "</td>";
Print "<td width='20%>&nbsp;</td>";
Print "<td width='18%'>Firstname : ".$info['person_firstname'] . "</td>";
Print "<td width='18%'>&nbsp;</td>";
Print "<td width='22%' rowspan='5'>Photo : " . header('Content-Type: image/jpeg') .$info['person_photo'] . "</td>";
Print "<td width='5% rowspan='7'>&nbsp;</td>";
Print "</tr>";
 
Print "</table>";
} 
 
[color=#BF0000]?>[/color]
Baronen
Forum Newbie
Posts: 1
Joined: Tue Oct 20, 2009 5:44 am

Re: Trying to display a blob ...

Post by Baronen »

You can't change the header after its sent to the browser.

Code: Select all

Print "<td width='22%' rowspan='5'>Photo : " . header('Content-Type: image/jpeg') .$info['person_photo'] . "</td>";
You are trying to set the header as a image/jpeg.

You have to create a new PHP file that has the header image/jpeg.

display_image.php

Code: Select all

 
$person_country = $_GET['person'];
//Make an sql to get the image
header('Content-Type: image/jpeg');
 
echo $info['person_photo'];
 
 
And then

Code: Select all

 
Print "<td width='22%' rowspan='5'>Photo : <img src='display_image.php?person=81' /></td>";
 
Maybe not the best solutions. But it works
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Trying to display a blob ...

Post by AbraCadaver »

You can't just display the blob in the inline HTML, you have to do it the way images are done in HTML. Something like this (untested):

image.php

Code: Select all

// change this to whatever you have as primary key
$id = (int)$_GET['id'];
$query = "SELECT * FROM people WHERE person_id = $id";
$data = mysql_query($query);
$info = mysql_fetch_array($data);
 
header('Content-type: image/jpeg');
$info['person_photo'];
line 18 in your code

Code: Select all

<!-- change $info['person_id'] to your primary key
Print "<td width='22%' rowspan='5'>Photo: <img src='image.php?id=" . $info['person_id'] . "' alt='whatever' /></td>";
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
stirnpanzer
Forum Newbie
Posts: 7
Joined: Tue Jun 02, 2009 6:38 am

Re: Trying to display a blob ...

Post by stirnpanzer »

image.php :

Code: Select all

<?php
// change this to whatever you have as primary key
$id = (int)$_GET['id'];
$query = "SELECT * FROM people WHERE id = $id";
$data = mysql_query($query);
$info = mysql_fetch_array($data);
 
header('Content-type: image/jpeg');
$info['person_photo'];
 
?>
display_person.php

Code: Select all

Print "<td width='22%' rowspan='5'>Photo: <img src='image.php?id=" . $info['person_id'] . "' alt='Picture here' /></td>";
 
It is retuning a broken image link... but with with all the correct info in the source ...

"http://localhost/xampp/mydb/image.php?id=10"

where 10 = person_id, it is showing the correct details for person 10 , eg: name etc...

and there is an image in the blob field "person_photo" for person 10

Any ideas..
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Trying to display a blob ...

Post by AbraCadaver »

Well damn. I omitted the echo in the image.php:

$info['person_photo'];

should be:

echo $info['person_photo'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply