0

I would like to know how to display an image stored in a database inside of a particular div using PHP code?

The code I used looks like that:

<form method="post">
<input type="button" name="show" value="show"/><br/>
<input type="image" id="image_show" name="img" value="img"/>
</post>

if(@$_POST['show'])
    {
        $sql="select imageData form images ORDER BY DESC";
        $result=mysql_query($sql) or die('invalid query'.mysql_error());
        //set header
        header("Content-type:image/png");
        echo mysql_result($result,0);
        while( $row = mysql_fetch_row( $result ) )
        {
            echo "<img src='".$row[0]."'/>";
            }
        }

But it does not work. How can I solve this task?

1
  • 2
    First, we need to know how you save your image in database. Do you save the path or save the blob? What is the example of the value of your image column? Commented Apr 4, 2013 at 10:01

3 Answers 3

2

You need to specify the name of the column you want to retrieve from your returned array (imageData in this case)

echo '<img src="'.$row['imageData'].'"/>";

Also, if dont need this line if you just want to show the image and now force the download:

//set header
header("Content-type:image/png");

It will throw you an error anyway as you are printing information before it with that form.

Sign up to request clarification or add additional context in comments.

Comments

1
  echo "<img src='".$row['imageData']."'/>";

Comments

0

If the data is embedded in the database as blob, use:

<img src="data:image/png;base64,ABC"/>

With 'ABC' is the image in base64.

Please see here for more information on data URL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.