Reputation: 1
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?
Upvotes: 0
Views: 151
Reputation: 7779
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.
Upvotes: 0
Reputation: 41595
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.
Upvotes: 2