How can I access an image from mysql – PHP

  php

Q(Question):

Hi,

I have this script:

<?php
mysql_connect("host", "user", "pass");

mysql_select_db("database");

$query = "select * from table";
$result = MYSQL_QUERY($query);
while($myrow=mysql_fetch_array($result)){
echo "<img src=".$myrow[‘bin_data’]."><br>";
};
?>

I can’t get the output from the "bin_data" to show as an image ( which it
is ).
Can anyone tell me what’s wrong?

Ricki

A(Answer):

Ricki Susic wrote:

Hi,

I have this script:

<?php
mysql_connect("host", "user", "pass");

mysql_select_db("database");

$query = "select * from table";
$result = MYSQL_QUERY($query);
while($myrow=mysql_fetch_array($result)){
echo "<img src=".$myrow[‘bin_data’]."><br>";
};
?>

I can’t get the output from the "bin_data" to show as an image ( which it
is ).
Can anyone tell me what’s wrong?

You have to send the correct MIME Type header.
Matthias

A(Answer):

I noticed that Message-ID: <bn************@ID-16734.news.uni-berlin.de>
from Matthias Gutfeldt contained the following:

$result = MYSQL_QUERY($query);
while($myrow=mysql_fetch_array($result)){
echo "<img src=".$myrow[‘bin_data’]."><br>";
};
?>

I can’t get the output from the "bin_data" to show as an image ( which it
is ).
Can anyone tell me what’s wrong?

You have to send the correct MIME Type header.

I find it easier to store the image elsewhere and just save the path in
the db.


Geoff Berrow
It’s only Usenet, no one dies.
My opinions, not the committee’s, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

A(Answer):

Geoff Berrow wrote:

I noticed that Message-ID: <bn************@ID-16734.news.uni-berlin.de>
from Matthias Gutfeldt contained the following:

$result = MYSQL_QUERY($query);
while($myrow=mysql_fetch_array($result)){
echo "<img src=".$myrow[‘bin_data’]."><br>";
};
?>

I can’t get the output from the "bin_data" to show as an image ( which it
is ).
Can anyone tell me what’s wrong?

You have to send the correct MIME Type header.

I find it easier to store the image elsewhere and just save the path in
the db.

Me too. But that’s not what he’s doing.
Matthias

A(Answer):

>You have to send the correct MIME Type header.

Matthias

Can you tell what that is?

Ricki

A(Answer):

Hello,

To use an image, you nead a header-information.
You can’t create an image in a file you are using.
Just use an other file that takes the image-data and sets a header
like "header("Content-Type: …");

Than you can use the image by using filename.php?img=DB-ID

sorry, my english is not very good 🙂

Kilian

A(Answer):

Ricki Susic wrote:

You have to send the correct MIME Type header.
Matthias

Can you tell what that is?

The important thing to note about image creation: The PHP code that
creates the image has to be in a file on its own, which is then called
from the page: <img src="php-created-image.php">. You can’t "echo" the
image date right into the src attribute.

Check the PHP manual: <http://ch.php.net/manual/en/ref.image.php>.
There’s a simple example for a PNG graphic, and an explanation how to
use it.
Matthias

LEAVE A COMMENT