|
Posted by kenoli on 12/24/06 17:17
So, let me see if I get this. I would create a script (view.php) that
goes something like:
<?php
define ('DB_USER', 'user');
define ('DB_PASSWORD', 'password');
define ('DB_HOST', 'localhost');
define ('DB_NAME', 'database');
//Make the connection and then select the database.
global $dbc;
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die
('Could not connect to MySQL: ' .
mysql_error() );
@mysql_select_db (DB_NAME) OR die
('Could not select database: ' .
mysql_error() );
$sql = "SELECT image FROM tbl_person WHERE
person_id=$_GET[person_id]";
// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " .
mysql_error());
header("Content-type: image/png");
echo mysql_result($result, 0);
?>
Then I place it in my display page using something like:
<img src="view.php?person_id=1" title="my dynamig image" alt="missing
image">
When I do this, the page shows up with an image symbol with a ? in it
and the source code showing the above tag. Obviously, it is not
getting the image data. Both files are in the same directory. There
is an "image" BLOB record in the tbl_person data base with person_id=1.
Where is my misunderstanding here?
--Kenoli
J.O. Aho wrote:
> kenoli wrote:
> > I want to store thumbnail images in a table with person data and then
> > retreive the image data and dislay it on a web page along with other
> > data about the person. All of the examples showing how to retrieve
> > image data from a blob field seem to assume that it will be displayed
> > on a separate page sending a header like this:
> >
> > Header( "Content-type: $type");
> > echo $data;
>
> To make it easy, we assume this script (what you call page) is called view.php
>
>
> > I'd like to retrieve the data and display it on a page in the middle of
> > other information like I would an image file stored in a directory,
> > e.g.
> >
> > <img src=[data from the blob field]>
>
> With the previous assumption
> <img src="view.php?id=<?php echo $imageid; ?>" title="my dynamig image"
> alt="missing image">
>
>
> > I don't really understand the Header . . . echo construction. I have
> > just seen examples of this but no explanations about is how this works.
>
> header is used to describe the contents of the data that is sent and is
> specially useful when you send image data instead of html.
>
> http://www.php.net/manual/en/function.header.php
>
>
>
> > I thought about perhaps retrieving the data from the blob and then
> > writing it to a file and then displaying the file using <img> but
> > couldn't figure out how to do that aside from thinking it may use
> > file_put_contents() somehow, though I couldn't figure out exactly how
> > to do it.
>
> Thats possible to do, specially on a low traffic site, but with increasing
> traffic you will just get high slowdowns and even risk of wrong images displayed.
>
>
> --
>
> //Aho
[Back to original message]
|